Jump to content

Recommended Posts

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 test
my table called : users
my records are called :

id int ( 11) AUTO_INNCREMENT
username , 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!!

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".
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.