dsl36367 2017-04-12 07:54
浏览 27
已采纳

PHP和MySQL,代码不起作用

I wanted to ask for help in a mini project, the project consists of updating two records of a MySQL database, I have a record that is called nom and another one that is called code, I want to enter a page html or Php (I do not care, either) and change the registry, for example, if I have a registry with the name Juan and with the code 123456, I want to be able to rename the name and the code, in The table there are only 2 columns the nom and the code.

I hope I explained correctly and thank you very much for the help.

Code1:

<?php
$servername = "localhost";
$username = "***";
$password = "***";
$dbname = "***";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT code, name FROM codes";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr><th>ID</th><th>Name</th></tr>";
    while($row = $result->fetch_assoc()) {
        echo    "<tr><td>".$row["code"]."</td>"
                "<td>".$row["name"]." </td>"
                "<td><a href='editar.php?id=".$row["code"].&.$row["name"]."'>Editar</a></td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}

$conn->close();
?>

Code2:

$username = "***";
$password = "***";
$dbname = "****";

$nom = $_GET["nom"]
$_GET["code"]

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "UPDATE nom, code SET WHERE name=$nom";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?>
  • 写回答

1条回答 默认 最新

  • du060334 2017-04-12 07:59
    关注

    There is multiple issue in your code,

    $name = $_GET["name"];
    $code = $_GET["code"]; // forget to assign it
    

    Also :

    <td><a href='editar.php?id=".$row["code"].&.$row["name"]."'>Editar</a></td></tr>;
    

    The "&" will not work, you need to do it like that I think :

    <td><a href='editar.php?code=".$row["code"]."&name=".$row["name"]."'>Editar</a></td></tr>;
    

    Add &name= before the $row["name"] and use code= before $row["code"] since you use $_GET["code"]; inside Php.

    There is also a problem with the update

    $sql = "UPDATE codes SET name = '".$name."', code = $code WHERE name= '".$nom."'";
    

    Additionnal information, I did not use quotes around $code since $code looks like an integer, and so, you do not need to convert it as a string.

    Btw, you should check that $name and $code are correct, cause if you use them like that, some SQL injection could be done. [Check @Twinfriends comments]

    Always check input

    Edit : As @Mubashar comments out, use for example : http://php.net/manual/en/function.filter-input.php

    Thanks to @Masivuye for is review.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?