开发者

Why is my data not submitted to MySQL database?

I have a PHP engine page which processes the data entered on a form with variable rows. The PHP generates an email and should also enter the data into a MySQL table which is already created. The email is working fine, but for some reason no data is getting entered in my MySQL Database. I am sure that all of the settings for the database and table are correct. I am also not seeing any error messages. My code consists of the following:

html for form:

<form method="post" name="booking" action="bookingengine.php">
    <fieldset>
        <h2>Waged/Organisation Rate</h2>
       <p>
            <input type="text" name="name[]">
            <input type="text" name="email[]">
            <input type="text" name="organisation[]">
            <input type="text" name="position[]">
        </p>
        <p><span class="add">Add person</span></p>
    </fieldset>

    <fieldset>
        <h2>Unwaged Rate</h2>
        <p>
            <input type="text" name="name2[]">
            <input type="text" name="email2[]">
        </p>
        <p><span class="add">Add person</span></p>
    </fieldset>

    <p><input type="submit" name="s开发者_运维技巧ubmit" id="submit" value="Submit and proceed to payment page" class="submit-button" /></p>

</form>

connection.php:

<?php

$hostname = "localhost";
$database = "****";
$username = "****";
$password = "****";
$connection = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);

?>

bookingengine.php:

<? include 'connection.php'; ?>

<?php

$emailFrom = "****";
$emailTo = "****";
$subject = "Booking for Soteria Conference";

$body = "The following people have booked for the Soteria Conference in Derby:" . "\n\n" . "Waged/Organisation Rate:" . "\n\n";
$row_count = count($_POST['name']);
$row_count2 = count($_POST['name2']);

 $values = array();

 for($i = 0; $i < $row_count; $i++) {
     // variable sanitation...
     $name = trim(stripslashes($_POST['name'][$i]));
     $email = trim(stripslashes($_POST['email'][$i]));
     $organisation = trim(stripslashes($_POST['organisation'][$i]));
     $position = trim(stripslashes($_POST['position'][$i]));

     // this assumes name, email, and telephone are required & present in each element
     // otherwise you will have spurious line breaks. 
     $body .= "Name: " . $name . "    Email: " . $email . "  Organisation: " . $organisation . "   Position: " . $position . "\n\n";

     //prepare the values for MySQL
     $values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')';
}

mysql_select_db($database, $connection);

$query = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES " . implode(',', $values);

 $values = array();

 $body .= "Unwaged Rate:" . "\n\n";

 for($i = 0; $i < $row_count; $i++) {
     // variable sanitation...
     $name = trim(stripslashes($_POST['name'][$i]));
     $email = trim(stripslashes($_POST['email'][$i]));

     // this assumes name, email, and telephone are required & present in each element
     // otherwise you will have spurious line breaks. 
     $body .= "Name: " . $name . "    Email: " . $email . "\n\n";

     //prepare the values for MySQL
     $values[] = '(' . $name . ',' . $email . ')';
}
$query = "INSERT INTO conference (Name, Email) VALUES " . implode(',', $values);

// send email 
$success = mail($emailTo, $subject, $body, "From: <$emailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=/conference/payment.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

Can anyone spot why the data isn'y getting sent to the MySQL database?

Thanks,

Nick

Current Code:

for($i = 0; $i < $row_count; $i++) {
     // variable sanitation...
     $name = trim(stripslashes($_POST['name'][$i]));
     $email = trim(stripslashes($_POST['email'][$i]));
     $organisation = trim(stripslashes($_POST['organisation'][$i]));
     $position = trim(stripslashes($_POST['position'][$i]));

     // this assumes name, email, and telephone are required & present in each element
     // otherwise you will have spurious line breaks. 
     $body .= "Name: " . $name . "    Email: " . $email . "  Organisation: " . $organisation . "   Position: " . $position . "\n\n";

     //prepare the values for MySQL
     $values = "'" . $name . "','" . $email . "','" . $organisation . "','" . $position . "'";
}

mysql_select_db($database, $connection);

$query1 = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES " . $values;

$result1 = mysql_query($query1);
if (!$result1) {
  die('Invalid query: ' . mysql_error());
}


I guess you didn't execute your queries after setting your insert statements.


The mysql_query($query); or another function which executes the query is missing. You are building your query with $query = ... but then you don't use this variable.

You could add something like this:

$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

For more Details see: http://www.php.net/manual/en/function.mysql-query.php

Additionally the query is missing some apostrophes for the values:

Your Code:

 $values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')';

Should be this:

 $values[] = '(\'' . $name . '\',\'' . $email . '\',\'' . $organisation . '\',\'' . $position . '\')';

or

 $values[] = "('" . $name . "','" . $email . "','" . $organisation . "','" . $position . "')";

Otherwise the values would be interpreted as field names.


Take for instance your first query value...

$query = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES (" . $values . ")";

This isn't actually executing the query, but simply defining it. You have to take the extra step of executing it by passing it through mysql_query() like this...

mysql_query($query);

Needless to say, you should probably look into sterilizing your data before inserting it into a database. Good luck!

Edit: Change this line...

$values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')';

To this. As you can see, you need quotes around your individual values, and there's no need in this instance for an array. Just use a string. You can also remove implode() from your query string and just reference the variable.

$values = "('" . $name . "','" . $email . "','" . $organisation . "','" . $position . "')";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜