Herbert at GC Posted April 28, 2011 Report Share Posted April 28, 2011 Hi Guys Help! This topic is from Complete Web Programmer tutorial (FYI the tutorial is great!). I'm trying to add a new row to the view.php page, I inserted a new row in PHPMyAdmin. I tweaked the codes (record.php), but it won't add the new record. The code currently use if statement to check for rows like this "if($name =='' || $last =='')", my code "if($name =='' || $last =='' || $mycode)" could the be the problem? I used OR 3x within if statement.??? Thank you, -- Herbert at GC Quote Link to comment Share on other sites More sharing options...
falkencreative Posted April 28, 2011 Report Share Posted April 28, 2011 Your code if($name =='' || $last =='' || $mycode)" currently reads: "if name is null, or last is null, or mycode is true... continue" I'm guessing it needs to be: if($name =='' || $last =='' || $mycode == '') Quote Link to comment Share on other sites More sharing options...
Herbert at GC Posted April 28, 2011 Author Report Share Posted April 28, 2011 Your code if($name =='' || $last =='' || $mycode)" currently reads: "if name is null, or last is null, or mycode is true... continue" I'm guessing it needs to be: if($name =='' || $last =='' || $mycode == '') Hi Ben, it is actually if($name =='' || $last == '' || $phone == ''). I wonder if it's OK to use OR 3x? I don't want to post the whole codes I'm tying to solve it my self. Thanks! Herbert at GC. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted April 28, 2011 Report Share Posted April 28, 2011 Yes, it is fine to use 3 "or's". That isn't the issue. I think if you need further help with this, you'll need to post the code. There isn't too much I can do to help otherwise (other than just trying to guess at the issue.) Quote Link to comment Share on other sites More sharing options...
Herbert at GC Posted April 29, 2011 Author Report Share Posted April 29, 2011 Yes, it is fine to use 3 "or's". That isn't the issue. I think if you need further help with this, you'll need to post the code. There isn't too much I can do to help otherwise (other than just trying to guess at the issue.) Hi Ben: code below. ============================================================== <link rel="stylesheet" href="crud-css.css" type="text/css" /> <?php /* Allows the user to both create new records and edit existing records */ // connect to the database include('crud-connect.php'); // creates the new/edit record form // since this form is used multiple times in this file, I have made it a function that is easily reusable function renderForm($company_name = '', $contact ='', $phone ='', $error = '', $id = '') { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title> <?php if ($id != '') { echo "Edit Record"; } else { echo "Add Record"; } ?> </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </head> <body> <h1><?php if ($id != '') { echo "Edit Record"; } else { echo "Add 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 ($id != '') { ?> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <p>ID: <?php echo $id; ?></p> <?php } ?> <table> <tr> <td><strong>Company Name:</td> <td></strong> <input type="text" name="company_name" value="<?php echo $company_name; ?>"/> *</td> </tr> <tr> <td><strong>Contact Name:</td> <td></strong> <input type="text" name="contact" value="<?php echo $contact; ?>"/> *</td> <tr> <td><strong>Phone:</td> <td></strong> <input type="text" name="phone" value="<?php echo $phone; ?>"/> *</td> </tr> <tr> <td> </td> <td>*Required <input type="submit" name="submit" value="Submit" /></td> </tr> </table> </div> </form> </body> </html> <?php } /* EDIT RECORD */ // if the 'id' variable is set in the URL, we know that we need to edit a record if (isset($_GET['id'])) { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // make sure the 'id' in the URL is valid if (is_numeric($_POST['id'])) { // get variables from the URL/form $id = $_POST['id']; $company_name = htmlentities($_POST['company_name'], ENT_QUOTES); $contact = htmlentities($_POST['contact'], ENT_QUOTES); $phone = htmlentities($_POST['phone'], ENT_QUOTES); // check that company_name and contact are both not empty if ($company_name == '', $contact == '', $phone == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($company_name, $contact, $error, $phone, $id); } else { // if everything is fine, update the record in the database if ($stmt = $mysqli->prepare("UPDATE client_list SET company_name=?, contact=?, phone=?, WHERE id=?")) { $stmt->bind_param("ssi", $company_name, $contact, $phone, $id); $stmt->execute(); $stmt->close(); } // show an error message if the query has an error else { echo "ERROR: could not prepare SQL statement."; } // redirect the user once the form is updated header("Location: crud-view.php"); } } // if the 'id' variable is not valid, show an error message else { echo "Error!"; } } // if the form hasn't been submitted yet, get the info from the database and show the form else { // make sure the 'id' value is valid if (is_numeric($_GET['id']) && $_GET['id'] > 0) { // get 'id' from URL $id = $_GET['id']; // get the recod from the database if($stmt = $mysqli->prepare("SELECT * FROM client_list WHERE id=?")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($id, $company_name, $contact, $phone); $stmt->fetch(); // show the form renderForm($company_name, $contact, $phone, NULL, $id); $stmt->close(); } // show an error if the query has an error else { echo "Error: could not prepare SQL statement"; } } // if the 'id' value is not valid, redirect the user back to the view.php page else { header("Location: view.php"); } } } /* NEW RECORD */ // if the 'id' variable is not set in the URL, we must be creating a new record else { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // get the form data $company_name = htmlentities($_POST['company_name'], ENT_QUOTES); $contact = htmlentities($_POST['contact'], ENT_QUOTES); $phone = htmlentities($_POST['phone'], ENT_QUOTES); // check that company_name and contact are both not empty if ($company_name == '' || $contact == '' || $phone == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($company_name, $contact, $phone, $error); } else { // insert the new record into the database if ($stmt = $mysqli->prepare("INSERT client_list (company_name, contact, phone) VALUES (?, ?, ?)")) { $stmt->bind_param("ss", $company_name, $contact, $phone); $stmt->execute(); $stmt->close(); } // show an error if the query has an error else { echo "ERROR: Could not prepare SQL statement."; } // redirec the user header("Location: crud-view.php"); } } // if the form hasn't been submitted yet, show the form else { renderForm(); } } // close the mysqli connection $mysqli->close(); ?> Thanks, Herbert Quote Link to comment Share on other sites More sharing options...
falkencreative Posted April 29, 2011 Report Share Posted April 29, 2011 Two points (I'm not sure if these are causing the issue, but definitely take a look): if ($company_name == '', $contact == '', $phone == '') I'm pretty sure you need to be using "&&" or "||" -- commas (I believe) should cause an error. and also in this section: if ($stmt = $mysqli->prepare("INSERT client_list (company_name, contact, phone) VALUES (?, ?, ?)")) { $stmt->bind_param("ss", $company_name, $contact, $phone); I believe the bind_param() function needs to use "sss" rather than "ss", since you are using three variables, not two. If neither of these things fix the issue, can you also post exactly what issue you are having? When you try to submit the form and create a new entry in the database, what happens? Nothing? Page refresh? Do you get any error messages? Quote Link to comment Share on other sites More sharing options...
Herbert at GC Posted April 29, 2011 Author Report Share Posted April 29, 2011 Two points (I'm not sure if these are causing the issue, but definitely take a look): if ($company_name == '', $contact == '', $phone == '') I'm pretty sure you need to be using "&&" or "||" -- commas (I believe) should cause an error. and also in this section: if ($stmt = $mysqli->prepare("INSERT client_list (company_name, contact, phone) VALUES (?, ?, ?)")) { $stmt->bind_param("ss", $company_name, $contact, $phone); I believe the bind_param() function needs to use "sss" rather than "ss", since you are using three variables, not two. If neither of these things fix the issue, can you also post exactly what issue you are having? When you try to submit the form and create a new entry in the database, what happens? Nothing? Page refresh? Do you get any error messages? Hi Ben bind_param()is the problem and the or worked. problem solved! Thanks man -- IOU Quote Link to comment Share on other sites More sharing options...
Herbert at GC Posted April 29, 2011 Author Report Share Posted April 29, 2011 Hi Ben bind_param()is the problem and the or worked. problem solved! Thanks man -- IOU This was also one of the problem. extra comma at the end of the "phone=?" bad: if ($stmt = $mysqli->prepare("UPDATE client_list SET company_name=?, contact=?, phone=?, WHERE id=?")) good: if ($stmt = $mysqli->prepare("UPDATE client_list SET company_name=?, contact=?, phone=? WHERE id=?")) - Herbert 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.