tyrone99 Posted December 4, 2014 Share Posted December 4, 2014 I have a problem .I 've been trying for a long time to make an update for php mysql to change the data.but every time I do not manage to make it work with a form.but it works if I only if I put this ($ sql = "UPDATE users SET username = 'value' WHERE id = 10 " ; )so it only works when I put the value of the id.but I want in an html form to indicate what I want to change and what id goes.but I have tried so long that I do not feel like I so want someone help me.make the same database and same as my records and make the code and test it if it works show me please my database name : web testmy table called : usersmy records are called :id int ( 11) AUTO_INNCREMENTusername , varchar ( 255 )password , varchar ( 255 )first_name , varchar ( 255 )last_name , varchar ( 255 )email, varchar ( 255 )Age, int ( 11) Look, my update.php is like this now<?php $servername = "localhost"; $username = "root"; $password = "....."; $dbname = "webtest"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "UPDATE users SET password='cotton candy' WHERE id=10"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } $conn->close(); ?> but now i have still have to go into the php file to change the valeu or the id but i looked on site and youtube how to put it in a simple html form but it still does not work. i want it in a html from. I want that when I enter the ID that the data of the user appears and that I can change any valeu separately. please help me out!! Quote Link to comment https://forums.phpfreaks.com/topic/292877-help-me-with-update-records-in-mysql-database/ Share on other sites More sharing options...
cyberRobot Posted December 4, 2014 Share Posted December 4, 2014 Have you worked with form variables before? If your form uses the POST method, you can access the form data with $_POST. Otherwise, you can use $_GET. To dynamically change the query based on an ID passed through the form, you could try something like this: <?php //... //GET THE ID FROM THE FORM if(isset($_POST['userID'])) { $id = $_POST['userID']; } else { $id = ''; } //IF PASSED ID IS A NUMBER, RUN QUERY if(ctype_digit((string)$id)) { $sql = "UPDATE users SET password='cotton candy' WHERE id=$id"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } //ELSE...INVALID ID } else { echo 'Invalid ID'; } //... ?> Note that the above code assumes that you are using the POST method on your form tag. And the field which passes the user's ID is named "userID". Quote Link to comment https://forums.phpfreaks.com/topic/292877-help-me-with-update-records-in-mysql-database/#findComment-1498481 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.