zhafran Posted March 24, 2011 Report Share Posted March 24, 2011 Hello all, Currently I'm working with Ben Falk's tutorial on PHP CRUD Videos and I got my head spinning with some errors I'm stuck at video part 5 and below is my code (which I did based on the videos) for your reference (to help me ): <?php include ('connect-db.php'); function renderForm($first = NULL, $last = NULL, $error = NULL, $id = NULL) { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?></title> <link href="css/style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="js/jquery.js"></script> <link rel="shortcut icon" href="images/favicon.ico" /> </head> <body> <h1><?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1> <?php if($error != '') { echo "<div style='padding=4px; border=1px solid red; color:red'>" . $error . "</div"; } ?> <form action="" method="post"> <div> <?php if (!empty($id)) { ?> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <p>ID: <?php echo $id; ?></p> <?php } ?> <strong>First Name: *</strong> <input type="text" name="firstname" value="<?php echo $first; ?>" /> <strong>Last Name: *</strong> <input type="text" name="lastname" value="<?php echo $last; ?>" /> <p>* Required</p> <input type="submit" name="submit" value="Submit" /> </div> </form> </body> </html> <?php } ?> <?php if (isset($_GET['id'])) { // editing existing records renderForm(NULL, NULL, NULL, $_GET['id']); } else { // create new record if (isset($_POST['submit'])) { $firstname = htmlentities($_POST['firstname'], ENT_QUOTES); $lastname = htmlentities($_POST['lastname'], ENT_QUOTES); if ($firstname == NULL || $lastname = NULL) { $error = 'ERROR: Please fill in all the required field'; renderForm($firstname, $lastname, $error); } else { if ($stmt = $mysqli->prepare("INSERT players (firstname, lastname) VALUES (?,?)")) { $stmt->bind_param("ss", $firstname, $lastname); $stmt->execute(); $stmt->close(); } else { echo "Error: Could not prepare SQL statement."; } header("Location: view.php"); } } else { renderForm(); } } $mysqli->close(); ?> And this is the error message I've got : Warning: mysqli_stmt::execute() [mysqli-stmt.execute]: (23000/1048): Column 'lastname' cannot be null in C:\xampp\htdocs\test\crud-mysqli\records.php on line 59 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\crud-mysqli\records.php:42) in C:\xampp\htdocs\test\crud-mysqli\records.php on line 64 Hopefully someone can help me on this and your help will highly appreciated! Many thanks! Quote Link to comment Share on other sites More sharing options...
zhafran Posted March 26, 2011 Author Report Share Posted March 26, 2011 Anybody home? Or do I need edit my question to be more specefic to get answer here? Thanks Quote Link to comment Share on other sites More sharing options...
falkencreative Posted March 26, 2011 Report Share Posted March 26, 2011 Sorry, must have missed seeing this one. I'll take a look. Quote Link to comment Share on other sites More sharing options...
Andrea Posted March 26, 2011 Report Share Posted March 26, 2011 I'm home - but don't know php, sorry. I'm sure Ben will help you out as soon as he sees your question - unless on of our other PHP experts beats him to it. Quote Link to comment Share on other sites More sharing options...
zhafran Posted March 26, 2011 Author Report Share Posted March 26, 2011 Sorry, must have missed seeing this one. I'll take a look. Thanks Ben, I tried many times to fix this 2 issue here ('header' and 'last name cannot be NULL') but really don't have an idea what's going wrong. Really appreciate your help! Quote Link to comment Share on other sites More sharing options...
zhafran Posted March 26, 2011 Author Report Share Posted March 26, 2011 I'm home - but don't know php, sorry. I'm sure Ben will help you out as soon as he sees your question - unless on of our other PHP experts beats him to it. Thanks Andrea to spend your time replying my topic. Although you not answering my question but it's great to see some reply from expert in noob's thread like me Quote Link to comment Share on other sites More sharing options...
falkencreative Posted March 26, 2011 Report Share Posted March 26, 2011 I believe the line that is causing the issue is here: if ($firstname == NULL || $lastname = NULL) { You need to have two "=" next to that last statement, otherwise you are accidentally setting $lastname to null. Try this and get back to me -- it should fix your problem. Quote Link to comment Share on other sites More sharing options...
zhafran Posted March 26, 2011 Author Report Share Posted March 26, 2011 I believe the line that is causing the issue is here: if ($firstname == NULL || $lastname = NULL) { You need to have two "=" next to that last statement, otherwise you are accidentally setting $lastname to null. Try this and get back to me -- it should fix your problem. Thanks Ben! The error gone already now One more thing, when I try to add new record, I got this error on records.php Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\crud-mysqli\records.php:42) in C:\xampp\htdocs\test\crud-mysqli\records.php on line 64 Is it because the doctype that I use that cause this error? Quote Link to comment Share on other sites More sharing options...
falkencreative Posted March 27, 2011 Report Share Posted March 27, 2011 I imagine that it is caused by the empty space just after your renderForm function, but before the start of the next code: <?php } ?> <?php I believe that empty line is being sent to the browser as a space character, which will cause the header() error when you try to redirect. Try revising those lines to this: <?php } ?> <?php or you could just drop the unnecessary closing/opening PHP statements and go with this: <?php } Quote Link to comment Share on other sites More sharing options...
zhafran Posted March 27, 2011 Author Report Share Posted March 27, 2011 I imagine that it is caused by the empty space just after your renderForm function, but before the start of the next code: <?php } ?> <?php I believe that empty line is being sent to the browser as a space character, which will cause the header() error when you try to redirect. Try revising those lines to this: <?php } ?> <?php or you could just drop the unnecessary closing/opening PHP statements and go with this: <?php } Nah! The error all fixed now and I learned something new today Thanks a lot Ben! You're really the legend here 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.
Note: Your post will require moderator approval before it will be visible.