user1822825
user1822825

Reputation: 51

PHP + MySQL Creating a form to change data in MySQL tables

I want to build a form to change the data in MySQL table. Firstly, I list all the data in the adminindex.php page. Then, I create a button to open the selected data in a form. I've done assigning the form fields to the main (pk) MySQL table. My problem started there when I need to fetch the foreign table data as the table contains many foreign data. As you guys know, a class may have many students, I have created the fields for class data, now the problem is in students data. Do I have to create many fields to fetch the data from MySQL foreign tables? If yes, could you guys guid me the code steps ? Thank you very much. Really appreciate your help :D

These are my steps:

Firstly I echo the rows, then I codes the form actions. Then, in adminpost.php, I create variables, link the fields and use UPDATE MYSQL to update the data in tables. I've succeeded in updating the primary table data but I'm stuck in foreign key data. Thanks :D

Upvotes: 2

Views: 14272

Answers (2)

Have 2 pages. Display data in a form in first one and have update in the second. Here is a code for doing it one by one, you can build on it for multiple rows at a time if you want to.

edit.php

<?php
mysql_connect('ip', 'username', 'password') or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());

$query = mysql_query("SELECT * FROM table1 where order by question_id limit 1") or             die(mysql_error());

if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
    $id = $row['id'];
    $value1= $row['value1'];
    $value2= $row['value2'];
}
?>
<form action="update.php" method="post">
<input type="hidden" name="ID" value="<?php echo $id;?>">

Value1: <input type="text" name="value1" value="<?php echo $value1;?>">
<br>
Value2: <input type="text" name="value2" value="<?php echo $value2?>">
<input type="Submit" value="Change">
</form>
<?php
}else{
    echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>

update.php

 <?php
 mysql_connect('ip', 'username', 'password') or die(mysql_error());
 mysql_select_db("db_name") or die(mysql_error());

 $id = mysql_real_escape_string($_POST["ID"]);
 $value1 = mysql_real_escape_string($_POST["value1"]);
 $value2 = mysql_real_escape_string($_POST["value2"]);

 $query="UPDATE table1 SET value1 = '.$value1.', value2 = '.$value2.' WHERE id='$id'";


 mysql_query($query)or die(mysql_error());
 if(mysql_affected_rows()>=1){
echo "<p>($id) Record Updated<p>";
 }else{
echo "<p>($id) Not Updated<p>";
 }
 ?>
 <a href="edit.php">Next</a>

Upvotes: 3

Dayn Goodbrand
Dayn Goodbrand

Reputation: 611

Might help to put your actual code up, but as i understand you are just wanting to edit the data that is already in your table? If thats the case :

//Connect to SQL DB
$dbcnx = @mysql_connect("localhost", "root", "password");

//Select DB
mysql_select_db("database_name", $dbcnx);

//Query DB
$sql = update table_name set column_name = "$variable" where column = "your_criteria";

@mysql_query($sql)

That will connect you to your SQL DB and update the records for you, hope thats what you needed

Upvotes: 0

Related Questions