如何优化我的代码,使其更快,更友好?

时间:2016-01-29 18:20:34

标签: php html mysql database localhost

<div id="database"> 
<form action="http://localhost/database/edit.php"  method="post">
<br>Please enter the ID of item you want to edit: <input type="text" name="ID" /><br><br>
What do you want to change:<br> <br>
<input type="radio"  name="change" value="Name"/>Name<br><br>
<input type="radio"  name="change" value="Cause" size="100" />Cause <br><br>
<input type="radio"  name="change" value="Symptom" size="200" />Symptom <br><br>
<input type="radio"  name="change" value="Gene_affected" size="200" />Gene_affected <br><br>
   Change it to: <input type="text" name="New" /><br><br>
<input type="submit" onclick="clicked(event)" />
</form>
</div> 

这是我的databse.php

   <?php
$con = mysql_connect("localhost","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

mysql_query("UPDATE pleasework SET $_POST[change] = '$_POST[New]'
WHERE ID='$_POST[ID]'");
echo "Change Successful<br>" ;
header("Location: databse.php");
mysql_close($con);
?>

这是我的edit.php

如何使用1个查询更改表格中的所有框? ////////////////////////////////////////////////// ////////

<div id="database"> 
<form action="http://localhost/database/edit.php"  method="post">
<br>Please enter the ID of item you want to edit: <input type="text" name="ID" /><br><br>
What do you want to change:<br> <br>
   Change Name to: <input type="text" name="New" /><br><br>
   Change Cause to: <input type="text" name="New1" /><br><br>
   Change Symptom to: <input type="text" name="New2" /><br><br>
   Change Gene_affected to: <input type="text" name="New3" /><br><br>
<input type="submit" onclick="clicked(event)" />
</form>
</div>

这是我的新主要php文件。我设法让它一次更新所有4个盒子,但它仍然如此不可靠和缓慢。请帮忙吗?

mysql_select_db("database", $con);

mysql_query("UPDATE pleasework SET Name= '$_POST[New]' WHERE ID='$_POST[ID]'");
mysql_query("UPDATE pleasework SET Cause= '$_POST[New1]' WHERE ID='$_POST[ID]'");
mysql_query("UPDATE pleasework SET Symptom= '$_POST[New2]' WHERE ID='$_POST[ID]'");
mysql_query("UPDATE pleasework SET Gene_affected= '$_POST[New3]' WHERE ID='$_POST[ID]'");
 echo "Change Successful<br>" ;
 header("Location: databse.php");
 mysql_close($con);

有任何建议可以帮助我改进我的代码吗?谢谢!

2 个答案:

答案 0 :(得分:0)

不推荐使用

mysql_个函数,您应该考虑使用msqli_或pdo

以下是使用mysqli_

更新多个字段的方法
$conn = $mysqli->prepare("UPDATE pleasework SET 
   field1 = ?,  
   field2 = ?  
   WHERE ID = ?");
$conn->bind_param(
   $_POST['value1'],
   $_POST['value2'],
   $_POST['id']);
$conn->execute(); 
$conn->close();

既然您已经知道mysql_已被弃用,那么这就是您要寻找的解决方案:

$sql = "UPDATE pleasework SET column_name1='value', column_name2='value' WHERE ID='$_POST[ID]'";

答案 1 :(得分:0)

您是否考虑过使用复选框而不是无线电输入?例如:     

然后在PHP端你会有这个查询:

$change_fields = array('Name', 'Cause', 'Symptom', 'Gene_affected');
$query = '';
foreach(change_fields as $field) {
    if(isset($_POST[field])) {
        $query .= ",$_POST[field] = '$_POST[New]'";
    }
}
$query = rtrim(query, ',');

if(!empty($query)) {
     mysql_query("UPDATE pleasework SET $query WHERE ID=$_POST[ID]");")
}

通常情况下,我会超越这些值并使用某种包装数据库类,但在您对此感到满意之后,这是您想要做的事情。