Jump to content

can't insert records into database


attaboy

Recommended Posts

I created this database

<?php
$mysqli = mysqli_connect('localhost', 'admin', 'jce123', 'php_class');
if(mysqli_connect_errno()) {
printf("connection failed: %s\n", mysqli_connect_error());
exit();
}else{
$q = mysqli_query($mysqli, "DROP TABLE IF EXISTS  airline_survey");
	if($q){echo "deleted the table airline_survey....<br>";}
	else{echo "damm... ".mysqli_error($mysqli);}
	$sql = "CREATE TABLE airline_survey
			(
			id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
			staff CHAR(10) NOT NULL,
			luggage CHAR(10) NOT NULL,
			seating CHAR(10) NOT NULL,
			clean CHAR(10) NOT NULL,
			noise CHAR(10) NOT NULL 
			)"; 
		$res = mysqli_query($mysqli, $sql);
		if($res === TRUE) {
			echo "table created";
		} else {
			printf("Could not create table: %s\n", mysqli_error($mysqli));
		}
		mysqli_close($mysqli);
	}
?>

When I look at it it looks fine.

I have a form that sends data to this script:

<?php
$con = mysql_connect('localhost', 'admin', 'abc123');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("php_class", $con);	

foreach ($_POST as $key => $value) {
$staff = "";
$luggage = "";
$seating = "";
$clean = "";
$noise = "";

  switch($key){
  	case "staff":
$staff = $value;
break;
case "luggage":
$luggage = $value;
break;
case "seating":
$seating = $value;
break;
case "clean":
$clean = $value;
break;
case "noise":
$noise = $value;
break;
    default:
echo "we must be in the twilight zone";
}
echo $staff."<br>";
[color=red]	mysql_query("INSERT INTO airline_survey 
(staff, luggage, seating, clean, noise)
VALUES ($staff, $luggage, $seating, $clean, $noise)");[/color]
}

?>

as you can see right before the insert query I test one of the variables to see if it has the string I'm expecting and it does.

The problem is the script runs without giving me an error message but the data never gets inserted into the table. 

 

Link to comment
Share on other sites

Well, you might be checking ONE variable, but that doesn't mean there isn't another problem in the query. Heck, maybe the value you are checking isn't correct for the field you are trying to save it to.

 

But, there is a pretty easy way to find out what the problem is - check the error. First off, create your query as a variable, so if there is a problem you can echo it to the page. BUt, at least one of your problems is that you are not enclosing the values in quotes. If those values/fields are anything but numeric fields that will certainly create an error.

 

$query = "INSERT INTO airline_survey 
              (staff, luggage, seating, clean, noise)
          VALUES
          ('$staff', '$luggage', '$seating', '$clean', '$noise')";
$result = mysql_query($query);

if(!$result)
{
    echo "Query failed. Query:<br>$query<br>Error:<br>" . mysql_error();
}
else
{
    echo "Query succeeded";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.