anneb Posted February 4, 2009 Share Posted February 4, 2009 I am just learning php code, and know enough to be dangerous. I have the code to enter in customers into a database. The script runs with no errors, but it does not update the database. I also do not get the expected 'Customer added.' response. I am sure it is a simple error, but I can't find it. Can anyone help me with this? <?php if (isset($_POST['submit'])) { $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $email = $_POST['email']; $output_form = 'no'; if (empty($first_name) || empty($last_name) || empty($email)) { // We know at least one of the input fields is blank echo 'Please fill out all of the email information.<br />'; $output_form = 'yes'; } } else { $output_form = 'yes'; } if (!empty($first_name) && !empty($last_name) && !empty($email)) { $dbc = mysqli_connect('localhost', 'nwmarria_dbuser', 'password', 'nwmarria_customers') or die('Error connecting to MySQL server.'); $query = "INSERT INTO email_list (first_name, last_name, email) VALUES ('$first_name', '$last_name', '$email')"; mysqli_query($dbc, $query) or die ('Data not inserted.'); echo 'Customer added.'; mysqli_close($dbc); } if ($output_form == 'yes') { ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label for="first_name">First name:</label> <input type="text" id="first_name" name="first_name" /><br /> <label for="last_name">Last name:</label> <input type="text" id="last_name" name="last_name" /><br /> <label for="email">Email:</label> <input type="text" id="email" name="email" /><br /> <input type="submit" name="submit" value="Submit" /> </form> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/143817-trouble-with-updating-database-in-php-code-please-help/ Share on other sites More sharing options...
ngreenwood6 Posted February 4, 2009 Share Posted February 4, 2009 try this: <?php if (isset($_POST['submit'])) { $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $email = $_POST['email']; $output_form = 'no'; if (empty($first_name) || empty($last_name) || empty($email)) { // We know at least one of the input fields is blank echo 'Please fill out all of the email information.<br />'; $output_form = 'yes'; } } else { $output_form = 'yes'; } if (!empty($first_name) && !empty($last_name) && !empty($email)) { $dbc = mysqli_connect('localhost', 'nwmarria_dbuser', 'password', 'nwmarria_customers') or die('Error connecting to MySQL server.'); $query = "INSERT INTO email_list(first_name, last_name, email) VALUES('$first_name','$last_name','$email')"; mysqli_query($dbc, $query) or die ('Data not inserted.'); echo 'Customer added.'; mysqli_close($dbc); } if ($output_form == 'yes') { ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label for="first_name">First name:</label> <input type="text" id="first_name" name="first_name" /><br /> <label for="last_name">Last name:</label> <input type="text" id="last_name" name="last_name" /><br /> <label for="email">Email:</label> <input type="text" id="email" name="email" /><br /> <input type="submit" name="submit" value="Submit" /> </form> <?php } ?> From now on please use the code tags. Also if you are wondering what I changed it was the query in the values you cannot have spaces between the variables. Link to comment https://forums.phpfreaks.com/topic/143817-trouble-with-updating-database-in-php-code-please-help/#findComment-754658 Share on other sites More sharing options...
anneb Posted February 4, 2009 Author Share Posted February 4, 2009 Sorry about the code. It was my first post, so I did not know about that. I removed the blanks in the VALUES statement and I am still having the same issue Here is my database structure: id, int(11), auto increment first_name varchar(30) last_name varchar(30) email varchar(50) Here is the code as I have changed it per your instructions. I do not get any error at all, it just does not add anything to the database. I also have another odd problem with the code. Or perhaps the code isn't odd, but I am. In the if (isset($_POST['submit'])) section, if I don't put an underscore in the $_POST variable I get an error. But that seems wrong when the form does not have underscores in the variable name. <?php $db='381473_document_whiz'; $dbuser='docwhiz'; $dbpass='kirkland63'; $dbhost='mysql50-09.wc1.dfw1.stabletransit.com'; if (isset($_POST['submit'])) { $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $email = $_POST['email']; $output_form = 'no'; if (empty($first_name) || empty($last_name) || empty($email)) { // We know at least one of the input fields is blank echo 'Please fill out all of the email information.<br />'; $output_form = 'yes'; } } else { $output_form = 'yes'; } if (!empty($first_name) && !empty($last_name) && !empty($email)) { $dbc = mysqli_connect($dbhost,$dbuser,$dbpass,$db) or die('Error connecting to MySQL server.'); $query = "INSERT INTO users VALUES (0,'$first_name','$last_name','$email')"; mysqli_query($dbc, $query) or die ('Data not inserted.'); echo 'Customer added.'; mysqli_close($dbc); } if ($output_form == 'yes') { ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label for="firstname">First name:</label> <input type="text" id="firstname" name="firstname" /><br /> <label for="lastname">Last name:</label> <input type="text" id="lastname" name="lastname" /><br /> <label for="email">Email:</label> <input type="text" id="email" name="email" /><br /> <input type="submit" name="submit" value="Submit" /> </form> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/143817-trouble-with-updating-database-in-php-code-please-help/#findComment-754835 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.