webdevdea Posted October 17, 2013 Share Posted October 17, 2013 I cannot get my program to update or insert? Attached are my sql file as well as the php that calls the functions to do the work. also here is the link to it working, this is not a homework assignment!!! http://www.ctcsports.org/upload/Fall2013/CIST2352/900104329/Assignment29/view.php delete.phpStyles.cssview-paginated.phpview.phpit is something I am doing on my own with the assistance of the class server ( working on a math and science club site) Trying to get this sql stuff down.. those of you that have.. Hats off to you !!!! Please and thank you for the help.. // Create a MySQL table in the selected database $sql = "DROP TABLE IF EXISTS players"; mysql_query($sql); mysql_query("CREATE TABLE `players` ( `id` int(11) NOT NULL auto_increment, `firstname` varchar(32) NOT NULL, `addressname` varchar(32) NOT NULL, `phone` varchar(32) NOT NULL, PRIMARY KEY (`id`) )") or die(mysql_error()); echo "Table Created!"; ?> <?php /* Allows the user to both create new records and edit existing records */ // connect to the database include("connect-db.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($first = '', $address ='', $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 "New Record"; } ?> </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </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 pink; color:pink'>" . $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 } ?> <strong>Name: *</strong> <input type="text" name="firstname" value="<?php echo $first; ?>"/><br/> <strong>Address: *</strong> <input type="text" name="addressname" value="<?php echo $address; ?>"/> <strong>Phone: </strong> <input type="text" name="phone" value="<?php echo $phone; ?>"/> <p>* required</p> <input type="submit" name="submit" value="Submit" /> </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 //ENT_QUOTES - Decodes double and single quotes $id = $_POST['id']; $firstname = htmlentities($_POST['firstname'], ENT_QUOTES); $addressname = htmlentities($_POST['addressname'], ENT_QUOTES); $phone = htmlentities($_POST['phone'], ENT_QUOTES); // check that firstname and addressname are both not empty if ($firstname == '' || $addressname == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($firstname, $addressname, $phone, $error, $id); } else { // if everything is fine, update the record in the database if ($stmt = $mysqli->prepare("UPDATE players SET firstname = ?, addressname = ? WHERE id=?")) { $stmt->bind_param("ssi", $firstname, $addressname, $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: 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 players WHERE id=?")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($id, $firstname, $addressname, $phone); $stmt->fetch(); // show the form renderForm($firstname, $addressname, $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 $firstname = htmlentities($_POST['firstname'], ENT_QUOTES); $addressname = htmlentities($_POST['addressname'], ENT_QUOTES); $phone = htmlentities($_POST['phone'], ENT_QUOTES); // check that firstname and addressname are both not empty if ($firstname == '' || $addressname == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($firstname, $addressname, $phone, $error); } else { // insert the new record into the database if ($stmt = $mysqli->prepare("INSERT players (firstname, addressname, phone) VALUES (?, ?, ?)")) { $stmt->bind_param("ss", $firstname, $addressname, $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: view.php"); } } // if the form hasn't been submitted yet, show the form else { renderForm(); } } // close the mysqli connection $mysqli->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/283060-cant-get-it-to-update-or-insert-new-people/ Share on other sites More sharing options...
Solution Barand Posted October 17, 2013 Solution Share Posted October 17, 2013 your bind statements are wrong $stmt->bind_param("ssi", $firstname, $addressname, $phone, $id); // should be "sssi"; and your other is also missing "s" Quote Link to comment https://forums.phpfreaks.com/topic/283060-cant-get-it-to-update-or-insert-new-people/#findComment-1454301 Share on other sites More sharing options...
webdevdea Posted October 17, 2013 Author Share Posted October 17, 2013 Ok thanks, Let me try that out.. so the s is standing in for the data the user is going to input? Quote Link to comment https://forums.phpfreaks.com/topic/283060-cant-get-it-to-update-or-insert-new-people/#findComment-1454307 Share on other sites More sharing options...
Barand Posted October 17, 2013 Share Posted October 17, 2013 yes. In the case I quoted^^, you have string - string - string - integer parameters Quote Link to comment https://forums.phpfreaks.com/topic/283060-cant-get-it-to-update-or-insert-new-people/#findComment-1454309 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.