I have a Database "Checkthisup" a table "Users" I want to be able to change the records from the table by searching by user id. If you have the code to do this please help me. I want to enter the row id and then have all the information posted in a form so I can change it then send it back to the database. These are the fields of the database: id, name, phone, page, dob, email. Thank you in advance
The code to fetch the data and put in a form to be edited so it could be uploaded back to the database. Thank you
In PHP you'll need to create the connection via mysql_connect() and mysql_select_db(). Then if you want to update fields you'll be required to update via queries. Such as, mysql_query("UPDATE database SET field1=value, field2=value, field3=value WHERE idfield=idtoupdate") To insert mysql_query("INSERT INTO database (field1, field2, field3, field3) VALUES (value1, value2, value3, value4)") To select mysql_query("SELECT field1, field2, field3 FROM database WHERE idfield=idtoselect") All pretty easy, (These should be correct but it's been a while so tell me if there is a mistake!)
can all this be made into a couple files or do i need to have more than 2 files? I was thinking about a first file (ex: get_id.php) where the user will be asked to enter the id of the row and clicks submit he will be taken to file 2 (ex:update.php) in this file there will be a form where all information from that row will be placed. The after he modifies the fields that need to be modified he submits the form that will update the database. The problem I m having now is that I can build the html part of the script, but I don't have no idea how to get the information inside the php form. If you could please help me, I would be very grateful. Thank you
<? function connect(){ $host = "localhost"; $username = "database_username"; $pass = "passhere"; $db_name = "database_name"; mysql_connect("$host", "$username", "$pass")or die("cannot connect"); mysql_select_db($db_name)or die(mysql_error()); } connect(); $user = $_GET['user']; // Gets Username From Url $query="select * from User where user='$user' limit 1"; $result = mysql_query($query); $row = mysql_fetch_object($result); //Display Fetched Database Below echo $row->id; echo $row->name; echo $row->phone; echo $row->page; echo $row->dob; echo $row->email; /* This is example how to display the information in a form outside of PHP <form> <input type="text" class="text" name="name" value="<?=$row->name;?>"/> </form> */ //How to change the information that is there. mysql_query("UPDATE `User` SET name='$name', phone='$phone' WHERE user = '$user'"); ?> PHP: That should give you everything you need.
<?php # Connect to MySQL - MYSQL_HOST | MYSQL_USERNAME | MYSQL_PASSWORD mysql_connect('localhost','root','root')or die(mysql_error()); # Connect to Database - Checkthisup mysql_select_db('Checkthisup')or die(mysql_error()); # Check to see if the button has been clicked if(isset($_POST['submit'])) { # Send Update Query to Database mysql_query("UPDATE users SET username = '".mysql_real_escape_string($_POST['username'])."' WHERE id = '".mysql_real_escape_string($_POST['id'])."'")or die(mysql_error()); # Print out success echo 'User has been updated.'; } else { # Print out HTML form echo '<form method="POST" action="file_name_here.php"> <!-- user id you wanted --> User Id: <input type="text" name="id"><br /> <!-- user name as example data, name can be accessed from $_POST['username'] --> Username: <input type="text" name="username"><br /> <input type="button" name="submit" value="Update"> </form>'; } PHP: