attaboy Posted March 16, 2012 Share Posted March 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 16, 2012 Share Posted March 16, 2012 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"; } Quote Link to comment Share on other sites More sharing options...
attaboy Posted March 16, 2012 Author Share Posted March 16, 2012 Yippie Skippi!! that did it!! I thought somehow that since the values were in variables that they didn't need to be enclosed in quotes...but I was wrong. Thank you so much Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.