开发者

edit and delet mysql table after enter id key in php

开发者 https://www.devze.com 2023-03-20 16:57 出处:网络
I need to edit a mysql record in php after entering an ID in field and selecting del / edit. enter ID (select edit / del)

I need to edit a mysql record in php after entering an ID in field and selecting del / edit.

  1. enter ID (select edit / del)
  2. get data from database
  3. after edit in fields then press submit
  4. update database successfully

I can't make ID request. Can any body help me to fix this?

update.php

<?php
$host="127.0.0.1"; // Host name
$username="code"; // Mysql username
$password="code2"; // Mysql password
$db_name="at_db"; // Database name
$tbl_name="persons"; // Table name

 mysql_connect("$host", "$username", "$password") or die(mysql_error());
 mysql_select_db("$db_name") or die(mysql_error());
 $data = mysql_query("SELECT * FROM persons WHERE id='id'")
 or die(mysql_error());
 Print "<table border cellpadding=3>";
 while($info = mysql_num_array( $data ))
?>
<table width="800" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Username</strong></td>
<td align="center"><strong>Password</strong></td>
<td align="center"><strong>ID Image</strong></td>
<td align="center"><strong>Firstname</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Birth Day</strong></td>
<td align="center"><strong>sex</strong></td>
<td align="center"><strong>company</strong></td>
<td align="center"><strong>web</strong></td>
<td align="center"><strong>E-Mail</strong></td>
<td align="center"><strong>Status</strong></td>
<td align="center"><strong>Certificate</strong></td>
<td align="center"><strong>Certificate Active</strong></td>
<td align="center"><strong>Certificat Expire</strong></td>
<td align="center"><strong>Score</strong></td>
<td align="center"><strong>Scop</strong></td>
<td align="center"><strong>Address</strong></td>
<td align="center"><strong>Tel</strong></td>
<td align="center"><strong>Fax</strong></td>

</tr>
<tr>
<td align="center"><input name="id"       type="text" id="id" value=""></td>
<td align="center"><input name="user"     type="text" id="user" value=""></td>
<td align="center"><input name="pass"     type="text" id="pass" value=""></td>
<td align="center"><input name="idimg"    type="text" id="idimg"value=""></td>
<td align="center"><input name="fname"    type="text" id="fname"value=""></td>
<td align="center"><input name="lname"    type="text" id="lname"value=""></td>
<td align="center"><input name="bday"     type="text" id="bday" value=""></td>
<td align="center"><input name="sex"      type="text" id="sex" value=""></td>
<td align="center"><input name="company"  type="text" id="company" value=""></td>
<td align="center"><input name="web"      type="text" id="web" value=""></td>
<td align="center"><input name="email"    type="text" id="email" value=""></td>

<td align="center"><input name="status"   type="text" id="status" value=""></td>
<td align="center"><input name="cer"      type="text" id="cer" value=""></td>
<td align="center"><input name="cerstart" type="text" id="cerstart" value=""></td>
<td align="center"><input name="cerexp"   type="text" id="cerexp" value=""></td>
<td align="center"><input name="score"    type="text" id="score" value=""></td>
<td align="center"><input name="scop"     type="text" id="scop" value=""></td>
<td align="center"><input name="address"  type="text" id="address" value=""></td>
<td align="center"><input name="tel"      type="text" id="tel" value=""></td>
<td align="center"&开发者_如何学Pythongt;<input name="fax"      type="text" id="fax" value=""></td>

</tr>
<tr>
<td>&nbsp;</td>
<td><input name="id" type="hidden" id="id" value="<? echo $row['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?

// close connection
mysql_close();

?>

update_ac.php

<?php
$host="127.0.0.1"; // Host name
$username="code"; // Mysql username
$password="code2"; // Mysql password
$db_name="at_db"; // Database name
$tbl_name="persons"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


// update data in mysql database
$sql="UPDATE $tbl_name SET id='$id', fname='$fname', lname='$lname',
username='$username', password='$password', idimg='$idimg', bday='$bday', sex='$sex',company='$company', web='$web', email='$email', status='$status', cer='$cer', cerstart='$cerstart', cerexp='$cerexp', score='$score', scop='$scop', address='$address', tel='$tel', fax='$fax' ";
$result=mysql_query($sql);


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

else {
    echo "ERROR";
}

?>


  1. There is no mysql_num_array function. You probably meant mysql_fetch_array.

  2. You have a form input named id, and also a hidden input named id, which is assigned a value from $row which is a nonexistant array. Is the ID supposed to come from the text input or from the hidden input?

  3. Your UPDATE query has no WHERE clause, so it will update every row in the table, not just the one with the given id. Add a WHERE clause.

  4. You have written no code to delete rows. It won't write itself.


Your code should only UPDATE - I don't see the code for delete.

However, are you getting any errors ?

Try modyfying your code:

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

else {
echo "ERROR";
}

with

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

else {
echo "ERROR".mysql_error();
}

And tell us if you get any errors

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号