6

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";
}

?>
2
  • are you getting required value at $rows['Customer']; ? Commented Apr 8, 2013 at 10:59
  • 1
    Your code leaves you open to SQL injection attacks. Look at the case of bobby tables for examples. Also, as mysql_* functions are deprecated please look at using MySQLi or PDO Commented Apr 8, 2013 at 11:04

3 Answers 3

8

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");
Sign up to request clarification or add additional context in comments.

1 Comment

@flov did you even look at the question? he explained that he will only use it local and that this is not a problem..
1
1.You have to pass a id when clicking a submit in update.php by
<a href="update_ac.php?id=<?php echo $rows['id']; ?>"><input type="submit" name="submit" value="Submit"></a>.

2.The line $id=$_GET['id'] is used in update_ac.php before insert query.

1 Comment

Thank you Mahendra, that worked a treat. Thanks to everyone for your advice too, much appreciated :)
0
$sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='id'" ;

this line is wrong you update it with STRING instead of integer. You should put

$sql="UPDATE ports SET Customer='Customer', Number='Number' WHERE id='".intval($_REQUEST['id'])."'" 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.