Jump to content

php and mysql


Sfrius
Go to solution Solved by Sfrius,

Recommended Posts

Hey, I'm trying to build a simple form with post back that is using a mysql database. I wanted to do a sticky post but the main file includes db creation commands. So I setup a process file to show the table I created and show the form below it so that I can keep adding additional records to the table. I'm currently running php v5.2.6.

Posting code: Notice how I made an insert string with $tmpvariables, does this work? I would think that it should.

My main problem came about when I tried to store the $_POST variables in temporary variables and then use an insert string as it is shown in the following code.

Any help is welcome and any additional comments are appreciated. Thank you.

<?php
$dbName = "airline";
$DBConnect = mysql_connect("127.0.0.1", "******", "*********************");
$createTable = "CREATE TABLE `surveys` (
 `f_date` VARCHAR( 30 ) NOT NULL ,
 `f_time` VARCHAR( 30 ) NOT NULL ,
 `f_number` VARCHAR( 30 ) NOT NULL ,
 `f_destination` VARCHAR( 30 ) NOT NULL ,
 `f_friend` VARCHAR( 30 ) NOT NULL ,
 `f_space` VARCHAR( 30 ) NOT NULL ,
 `f_comfort` VARCHAR( 30 ) NOT NULL ,
 `f_clean` VARCHAR( 30 ) NOT NULL ,
 `f_noise` VARCHAR( 50 ) NOT NULL
);";
if($DBConnect == FALSE){
 echo "<p>Connection Error: " . mysql_error() . "</p>\n";
}else {
 echo "<p>Connection Successful.</p>";
 $dropDB = "DROP DATABASE $dbName";
 $noDB = mysql_query($dropDB);
 $newDB = "CREATE DATABASE $dbName";
 $newResults = mysql_query($newDB);
 if($newResults == FALSE){
  echo "<p>Could not create the database. You don't have the required permissions or the database already exists." . mysql_error($DBConnect) . "</p>";
  }
 else{
  echo "<p>The database was created successfully.</p>";  
  if (mysql_select_db ("$dbName",$DBConnect) === FALSE){
   echo "<p>Could not select the database ", $dbName, ".", mysql_error($DBConnect), "</p>";
  }
  else{
   echo "<p>The ", $dbName, " database has been selected.";
   $QueryResult = mysql_query($createTable, $DBConnect);
   if($QueryResult === FALSE)
    echo "<p>There was an error or the table already exists in the database</p>";
   else{
    echo "<p>The table was created now to insert some data</p>";        
     
     echo "<h2>Survey</h2>";
     echo "<FORM action=\"Surveyprocess.php\" method=\"post\">";
     echo "<P>";
     echo "Flight Time: <INPUT type=\"text\" name=\"f_time\"><BR>";
     echo "Flight Date: <INPUT type=\"text\" name=\"f_date\"><BR>";
     echo "Flight Number: <INPUT type=\"text\" name=\"f_number\"><BR>";
     echo "Destination: <INPUT type=\"text\" name=\"f_destination\"><BR>";
     echo "<p>How would you rate the friendliness of the staff?</p>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the storage space on the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the comfort on the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the cleanliness of the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the noise level on the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Excellent\"> Excellent<BR>";
     
     echo "<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">";
     echo "</P>";
     echo "</FORM>";
     
   //  if(isset($_POST['Send'])){
     // $tmpVar1 = $_POST['f_time'];
      //$tmpVar2 = $_POST['f_date'];
     // $tmpVar3 = $_POST['f_number'];
     // $tmpVar4 = $_POST['f_destination'];
     // $tmpVar5 = $_POST['f_friend'];
     // $tmpVar6 = $_POST['f_space'];
     // $tmpVar7 = $_POST['f_comfort'];
     // $tmpVar8 = $_POST['f_clean'];
     // $tmpVar9 = $_POST['f_noise'];
     // $insert = "INSERT INTO `airline`.`surveys` (
      // `f_date` ,
       //`f_time` ,
       //`f_number` ,
       //`f_destination` ,
       //`f_friend` ,
       //`f_space` ,
       //`f_comfort` ,
       //`f_clean` ,
       //`f_noise` 
        //)
      //VALUES ($tmpVar1, $tmpVar2, $tmpVar3, $tmpVar4, $tmpVar5, $tmpVar6, $tmpVar7, $tmpVar8,  $tmpVar9);";
      //$insertQuery = mysql_query($insert, $DBConnect);
// if($insertQuery === FALSE){
  //  echo "<p>Survey failed to save to the database.</p>";
 //   }
  //else{
  //  echo "<p>Survey Complete!</p>";
 //  }
  //  $SQLSelect = "SELECT * FROM surveys";
  //  $QueryResult = mysql_query($SQLSelect, $DBConnect);
  //  echo "<table width='100' border='1'>\n";
  //  echo "<tr><th>Flight Date</th><th>Flight Time</th><th>Flight //Number</th><th>Destination</th><th>Friendliness</th><th>Storage Space</th><th>Comfort</th><th>Cleanliness</th><th>Noise</th></tr>\n";
 //   while (($Row = mysql_fetch_row($QueryResult)) !== FALSE) {
     //count $row array
     //for loop to iterate rows
 //    echo "<tr><td>{$Row[0]}</td>";
  //   echo "<td>{$Row[1]}</td>";
   //  echo "<td>{$Row[2]}</td>";
  //   echo "<td align='right'>{$Row[3]}</td>";
    // echo "<td>{$Row[4]}</td></tr>";
   // }
    // echo "</table>\n";
  //Sorry for this mess. was trying to post into the middle of my code thinking it would work. If I set it up to start with isset, it would be false initially
//The database would create, then everytime after isset would be true....maybe ill repost ina little while
  }
   }     
  }
 }
}
//mysql_close($DBConnet);
?>
 
action file.
 
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
 if(isset($_POST['Send'])){
 $tmpVar1 = $_POST['f_time'];
 $tmpVar2 = $_POST['f_date'];
 $tmpVar3 = $_POST['f_number'];
 $tmpVar4 = $_POST['f_destination'];
 $tmpVar5 = $_POST['f_friend'];
 $tmpVar6 = $_POST['f_space'];
 $tmpVar7 = $_POST['f_comfort'];
 $tmpVar8 = $_POST['f_clean'];
 $tmpVar9 = $_POST['f_noise'];
 
 $insert = "INSERT INTO `airline`.`surveys` (
  `f_date` ,
  `f_time` ,
  `f_number` ,
  `f_destination` ,
  `f_friend` ,
  `f_space` ,
  `f_comfort` ,
  `f_clean` ,
  `f_noise` 
  )
  VALUES ($tmpVar1, $tmpVar2, $tmpVar3, $tmpVar4, $tmpVar5, $tmpVar6, $tmpVar7, $tmpVar8,  $tmpVar9);";
  
 $createTable = "CREATE TABLE `surveys` (
  `f_date` VARCHAR( 30 ) NOT NULL ,
  `f_time` VARCHAR( 30 ) NOT NULL ,
  `f_number` VARCHAR( 30 ) NOT NULL ,
  `f_destination` VARCHAR( 30 ) NOT NULL ,
  `f_friend` VARCHAR( 30 ) NOT NULL ,
  `f_space` VARCHAR( 30 ) NOT NULL ,
  `f_comfort` VARCHAR( 30 ) NOT NULL ,
  `f_clean` VARCHAR( 30 ) NOT NULL ,
  `f_noise` VARCHAR( 50 ) NOT NULL
 );";
 $DBConnect = mysql_connect("127.0.0.1", "root", "noonan79");
 if($DBConnect == FALSE){
 echo "<p>Connection Error: " . mysql_error() . "</p>\n";
 }else {
 $insertQuery = mysql_query($insert, $DBConnect);
 if($insertQuery === FALSE){
    echo "<p>Survey failed to save to the database.</p>";
    }
   else{
    echo "<p>Survey Complete!</p>";
   
    $SQLSelect = "SELECT * FROM surveys";
    $QueryResult = mysql_query($SQLSelect, $DBConnect);
    echo "<table width='100' border='1'>\n";
    echo "<tr><th>Flight Date</th><th>Flight Time</th><th>Flight Number</th><th>Destination</th><th>Friendliness</th><th>Storage Space</th><th>Comfort</th><th>Cleanliness</th><th>Noise</th></tr>\n";
    //while (($Row = mysql_fetch_row($QueryResult)) !== FALSE) {
     //count $row array
     //for loop to iterate rows
    // echo "<tr><td>{$Row[0]}</td>";
    // echo "<td>{$Row[1]}</td>";
    // echo "<td>{$Row[2]}</td>";
    // echo "<td align='right'>{$Row[3]}</td>";
    // echo "<td>{$Row[4]}</td></tr>";
    //}
     echo "</table>\n";
     
     echo "<h2>Survey</h2>";
     echo "<FORM action=\"Surveyprocess.php\" method=\"post\">";
     echo "<P>";
     echo "Flight Time: <INPUT type=\"text\" name=\"f_time\"><BR>";
     echo "Flight Date: <INPUT type=\"text\" name=\"f_date\"><BR>";
     echo "Flight Number: <INPUT type=\"text\" name=\"f_number\"><BR>";
     echo "Destination: <INPUT type=\"text\" name=\"f_destination\"><BR>";
     echo "<p>How would you rate the friendliness of the staff?</p>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the storage space on the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the comfort on the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the cleanliness of the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the noise level on the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Excellent\"> Excellent<BR>";
     
     echo "<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">";
     echo "</P>";
     echo "</FORM>";
     }
   }
  }
?>
Edited by requinix
please use [code] tags when posting code
Link to comment
Share on other sites

This script is in some desperate need of variable sanitation, but other then some security quirks about using strings the query should work for you. Some questions though...

 

1.)Why do you have a declaration for $createtable in the second half of the script, when it's not called?

2.)Usually when I work on projects that require database creation, I'll remove that part to another file and use include. Makes it a bit easier on the eyes, and when the script goes to production it's usually just a matter of copy/paste. It also makes it easy to get back errors.

3.)On that note, what errors are you getting? Are you getting any errors?

4.)I'm also really not sure what you're looking for for help. You're post needs a bit of clarification... and Notepad++ does not like this code.

Link to comment
Share on other sites

No just a blank screen. No errors, let me repost the script for you, I have a good idea. Gonna do it now, should take an hour or so. I was trying different things so the code is out of whack. Gonna start from step 1 and work my way down with isset as the first if clause.

Link to comment
Share on other sites

You have PHP installed on your server or your host supports it, correct? You should at least be getting back a connection error if it's failing...

 

When you view the source(on the empty page), is there anything there?

 

I just ran the file, and on line 130 you have a extra "}". After that, I'm getting this for output:

Warning: mysql_connect(): Access denied for user '******'@'localhost' (using password: YES) in ***\testphpscript.php on line 3
Connection Error: Access denied for user '******'@'localhost' (using password: YES)
action file.
Edited by SitdGames
Link to comment
Share on other sites

Ok here it is 99% complete. I made it one file, but when I post back and use the same file to process it, it says that isset is false when it should be true?

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="content-type" content="text/html"; charset=iso-8859-1" />
</head>
<body>
<?php
$dbName = "airline";
$DBConnect = mysql_connect("127.0.0.1", "root", "yourpassword");
$createTable = "CREATE TABLE `surveys` (
 `f_date` VARCHAR( 30 ) NOT NULL ,
 `f_time` VARCHAR( 30 ) NOT NULL ,
 `f_number` VARCHAR( 30 ) NOT NULL ,
 `f_destination` VARCHAR( 30 ) NOT NULL ,
 `f_friend` VARCHAR( 30 ) NOT NULL ,
 `f_space` VARCHAR( 30 ) NOT NULL ,
 `f_comfort` VARCHAR( 30 ) NOT NULL ,
 `f_clean` VARCHAR( 30 ) NOT NULL ,
 `f_noise` VARCHAR( 50 ) NOT NULL
);";
if(isset($_POST['Send'])){
 $tmpVar1 = $_POST['f_time'];
 $tmpVar2 = $_POST['f_date'];
 $tmpVar3 = $_POST['f_number'];
 $tmpVar4 = $_POST['f_destination'];
 $tmpVar5 = $_POST['f_friend'];
 $tmpVar6 = $_POST['f_space'];
 $tmpVar7 = $_POST['f_comfort'];
 $tmpVar8 = $_POST['f_clean'];
 $tmpVar9 = $_POST['f_noise'];
 
 $insert = "INSERT INTO `airline`.`surveys` (
  `f_date` ,
  `f_time` ,
  `f_number` ,
  `f_destination` ,
  `f_friend` ,
  `f_space` ,
  `f_comfort` ,
  `f_clean` ,
  `f_noise` 
  )
  VALUES ($tmpVar1, $tmpVar2, $tmpVar3, $tmpVar4, $tmpVar5, $tmpVar6, $tmpVar7, $tmpVar8,  $tmpVar9);";
     
     $QueryResult = mysql_query($insert, $DBConnect);
     if($QueryResult === FALSE){
     echo "<p>Could not insert the data from $_POST</p>";
     }else{
      $SQLSelect = "SELECT * FROM surveys";
      $QueryResult = mysql_query($SQLSelect, $DBConnect);
      if($QueryResult === FALSE){
       echo "<p>There was an error selecting the data from the table</p>";
      }else{
       echo "<p>Selected data into a table</p>";     
    
       echo "<table width='100' border='1'>\n";
       echo "<tr><th>Flight Date</th><th>Flight Time</th><th>Flight Number</th><th>Destination</th><th>Friendliness</th><th>Storage Space</th><th>Comfort</th><th>Cleanliness</th><th>Noise</th></tr>\n";       
       while (($Row = mysql_fetch_row($QueryResult)) !== FALSE) {
        //was a note to myself.
        //count $row array
        //for loop to iterate rows
        echo "<tr><td>{$Row[0]}</td>";
        echo "<td>{$Row[1]}</td>";
        echo "<td>{$Row[2]}</td>";
        echo "<td align='right'>{$Row[3]}</td>";
        echo "<td>{$Row[4]}</td></tr>";
       }
       echo "</table>";
       echo "<h2>Survey ISSET IS TRUE</h2>";
      echo "<FORM action=\"airline.php\" method=\"post\">";
      echo "<P>";
      echo "Flight Time: <INPUT type=\"text\" name=\"f_time\"><BR>";
      echo "Flight Date: <INPUT type=\"text\" name=\"f_date\"><BR>";
      echo "Flight Number: <INPUT type=\"text\" name=\"f_number\"><BR>";
      echo "Destination: <INPUT type=\"text\" name=\"f_destination\"><BR>";
      echo "<p>How would you rate the friendliness of the staff?</p>";
      echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"No Opinion\"> No Opinion<BR>";
      echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Poor\"> Poor<BR>";
      echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Fair\"> Fair<BR>";
      echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Good\"> Good<BR>";
      echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Excellent\"> Excellent<BR>";
      echo "<p>How would you rate the storage space on the plane?</p>";
      echo "<INPUT type=\"radio\" name=\"f_space\" value=\"No Opinion\"> No Opinion<BR>";
      echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Poor\"> Poor<BR>";
      echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Fair\"> Fair<BR>";
      echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Good\"> Good<BR>";
      echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Excellent\"> Excellent<BR>";
      echo "<p>How would you rate the comfort on the plane?</p>";
      echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"No Opinion\"> No Opinion<BR>";
      echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Poor\"> Poor<BR>";
      echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Fair\"> Fair<BR>";
      echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Good\"> Good<BR>";
      echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Excellent\"> Excellent<BR>";
      echo "<p>How would you rate the cleanliness of the plane?</p>";
      echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"No Opinion\"> No Opinion<BR>";
      echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Poor\"> Poor<BR>";
      echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Fair\"> Fair<BR>";
      echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Good\"> Good<BR>";
      echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Excellent\"> Excellent<BR>";
      echo "<p>How would you rate the noise level on the plane?</p>";
      echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"No Opinion\"> No Opinion<BR>";
      echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Poor\"> Poor<BR>";
      echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Fair\"> Fair<BR>";
      echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Good\"> Good<BR>";
      echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Excellent\"> Excellent<BR>";
     
      echo "<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">";
      echo "</P>";
      echo "</FORM>";
     }
}
//end of ISSET and beginning of db creation and form display
}else{
 if($DBConnect == FALSE){
 echo "<p>Connection Error: " . mysql_error() . "</p>\n";
}else {
 echo "<p>Connection Successful.</p>";
 $dropDB = "DROP DATABASE $dbName";
 $noDB = mysql_query($dropDB);
 $newDB = "CREATE DATABASE $dbName";
 $newResults = mysql_query($newDB);
 if($newResults == FALSE){
  echo "<p>Could not create the database. You don't have the required permissions or the database already exists." . mysql_error($DBConnect) . "</p>";
  }
 else{
  echo "<p>The database was created successfully.</p>";  
  if (mysql_select_db ("$dbName",$DBConnect) === FALSE){
   echo "<p>Could not select the database ", $dbName, ".", mysql_error($DBConnect), "</p>";
  }
  else{
   echo "<p>The ", $dbName, " database has been selected.";
   $QueryResult = mysql_query($createTable, $DBConnect);
   if($QueryResult === FALSE)
    echo "<p>There was an error or the table already exists in the database</p>";
   else{
    echo "<p>The table was created now to insert some data</p>";     
    
     echo "<table width='100' border='1'>\n";
     echo "<tr><th>Flight Date</th><th>Flight Time</th><th>Flight Number</th><th>Destination</th><th>Friendliness</th><th>Storage Space</th><th>Comfort</th><th>Cleanliness</th><th>Noise</th></tr>\n";
     $SQLSelect = "SELECT * FROM surveys";
     $QueryResult = mysql_query($SQLSelect, $DBConnect);
     while (($Row = mysql_fetch_row($QueryResult)) !== FALSE) {
      //
      //count $row array
      //for loop to iterate rows
     echo "<tr><td>{$Row[0]}</td>";
     echo "<td>{$Row[1]}</td>";
     echo "<td>{$Row[2]}</td>";
     echo "<td align='right'>{$Row[3]}</td>";
     echo "<td>{$Row[4]}</td></tr>";
     }
     echo "</table>";
     echo "<h2>Survey ISSET IS FALSE</h2>";
     echo "<FORM action=\"airline.php\" method=\"post\">";
     echo "<P>";
     echo "Flight Time: <INPUT type=\"text\" name=\"f_time\"><BR>";
     echo "Flight Date: <INPUT type=\"text\" name=\"f_date\"><BR>";
     echo "Flight Number: <INPUT type=\"text\" name=\"f_number\"><BR>";
     echo "Destination: <INPUT type=\"text\" name=\"f_destination\"><BR>";
     echo "<p>How would you rate the friendliness of the staff?</p>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_friend\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the storage space on the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_space\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the comfort on the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_comfort\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the cleanliness of the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_clean\" value=\"Excellent\"> Excellent<BR>";
     echo "<p>How would you rate the noise level on the plane?</p>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"No Opinion\"> No Opinion<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Poor\"> Poor<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Fair\"> Fair<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Good\"> Good<BR>";
     echo "<INPUT type=\"radio\" name=\"f_noise\" value=\"Excellent\"> Excellent<BR>";
     
     echo "<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">";
     echo "</P>";
     echo "</FORM>";
}}}}}
//mysql_close($DBConnet);
?>
</body>
</html>
Link to comment
Share on other sites

It should. Your buttons need to have the (name=\'send\' ) line for it to send the data from the button during a refresh and sent to $_POST.

 

Also, please add some error checking into your mysql. You're returning a error from it:

 $QueryResult = mysql_query($insert, $DBConnect) or die("Mysql error:" . mysql_error());

And here's the error:

Notice: Undefined index: f_comfort in ***\testcode.php on line 30
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' Good, Excellent)' at line 12
Edited by SitdGames
Link to comment
Share on other sites

Alright, I tinkered with your MySql syntax.

$insert = "INSERT INTO `airline`.`surveys` (
  `f_date` ,
  `f_time` ,
  `f_number` ,
  `f_destination` ,
  `f_friend` ,
  `f_space` ,
  `f_comfort` ,
  `f_clean` ,
  `f_noise` 
  )
  VALUES (\"$tmpVar1\", \"$tmpVar2\", \"$tmpVar3\", \"$tmpVar4\", \"$tmpVar5\", \"$tmpVar6\", \"$tmpVar7\", \"$tmpVar8\", \"$tmpVar9\");";

This should solve the input problems, and then you need to fix the next query like so:

$SQLSelect = "SELECT * FROM `airline`.`surveys`";

Happy coding!

Link to comment
Share on other sites

  • Solution

Thanks sitd. I was so close, I knew it had something to do with my insert statement. I completely overlooked the fact that you can't have " inside of a string without an escape character.

The sql error was just saying that there was more data in the line than I was allowing to be displayed. I easily fixed this by adding row[5], 6,  7, and 8.

It took me 2 minutes to fix everything.

Edited by Sfrius
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.