Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php update sql from form

Tags:

php

mysql

I have a php script I'm trying to get working which I've basically just pulled from a tutorial and altered to suit my needs. This is my first attempt at php so please go easy on me.

I have 3 files

  1. list_records.php
  2. update.php
  3. update_ac.php

List_records reads data from a table in mysql. the table in list_records has an edit function which takes you to update.php where it displays the data in db table.

Update.php has a submit button which is meant to update mysql using update_ac.php with what ever info you changed using the id field in the url using $_GET['id].

I know this script is very open to slq injections but I'm planning to only use this in a local environment, it wont be exposed to the internet and only myself and one other person will be using this page so its not really an issue.

Anyway, I've confirmed a couple of things:-

  1. the id does get picked up using $_Get, i put in a echo and it printed it out on the update.php page.
  2. i can run the update command within the php and change values but it wont work when using $_GET[id]

Can anyone point me in the right direction?

here are the 3 files with the db connection details altered

list_records.php

<title>Ports</title>
</head>

<?php

// Connect to server and select database.
mysql_connect("localhost", "username", "passsword")or die("cannot connect"); 
mysql_select_db("porting")or die("cannot select DB");


$sql="SELECT * FROM ports";
$result=mysql_query($sql);

?>
<body>


<table width="1200" border="1" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="1200" border="1" cellspacing="1" cellpadding="3">
<tr>
<td colspan="50"><strong>Pending Port Requests 2</strong> </td>
</tr>

<tr>
<td align="center"><strong>Customer</strong></td>
<td align="center"><strong>Number</strong></td>
<td align="center"><strong>Type</strong></td>
<td align="center"><strong>Completed</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['Customer']; ?></td>
<td><?php echo $rows['Number']; ?></td>
<td><?php echo $rows['Type']; ?></td>
<td><?php echo $rows['Completed']; ?></td> 
<td align="center"><a href="update.php?id=<?php echo $rows['id']; ?>">update</a></td>
</tr>

<?php
}
?>

</table>
</td>
</tr>
</table>
</body>
</html>

update.php

<title>update</title>
</head>

<?php
// Connect to server and select database.
mysql_connect("localhost", "username", "password")or die("cannot connect"); 
mysql_select_db("porting") or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];



// Retrieve data from database 
$sql="SELECT * FROM porting.ports WHERE id = '$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<body>


<table width="1200" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="6"><strong>Update Porting Details</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Customer</strong></td>
<td align="center"><strong>Number</strong></td>
<td align="center"><strong>Type</strong></td>
<td align="center"><strong>Completed</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="Customer" type="text" id="Customer" value="<?php echo $rows['Customer']; ?>"size= "15"/>
</td>
<td align="center">
<input name="Number" type="text" id="Number" value="<?php echo $rows['Number']; ?>" size="15"/>
</td>
<td align="center">
<input name="Type" type="text" id="Type" value="<?php echo $rows['Type']; ?>" size="15"/>
</td>
<td align="center">
<input name="Comments" type="text" id="Completed" value="<?php echo $rows['Comments']; ?>" size="15"/>
</td>
<tr>
</table>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>"/>
<input type="submit" name="Submit" value="Submit" /></td>
<td align="center">&nbsp;</td>
</td>
</form>
</tr>
</table>
</body>
</html>

update_ac.php

<?php
// Connect to server and select database.
mysql_connect("localhost", "username", "password")or die("cannot connect"); 
mysql_select_db("porting")or die("cannot select DB");

// update data in mysql database 
$sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='id'" or die ("this stuffed up");
$result=mysql_query($sql) or die ("this stuffedup");


// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else {
echo "ERROR";
}

?>
like image 814
themoose Avatar asked Apr 08 '13 10:04

themoose


1 Answers

Your update query should be

// update data in mysql database 
$sql="UPDATE ports SET Customer='".$_POST['Customer']."', Number='".$_POST['Number']."' WHERE id='".$_POST['id']."'";

$result=mysql_query($sql)or 
die ("this stuffedup");
like image 102
Mahendra Avatar answered Sep 18 '22 11:09

Mahendra