PHP - 只更改一行,而不是全部

时间:2015-11-03 00:25:33

标签: php

我刚刚开始使用PHP,现在已经被困了5个小时直接试图解决这个问题!我理解发生了什么,但不能在我的生活中找到任何地方的修复D:基本上每一行都显示在用户浏览器上。除了每一个之外,还有一个标记为完整的'按钮。当按下此按钮时,将值从0更改为1.问题是,它将每行的值0更改为1 D:请帮助我,但是现在很麻烦了哈哈!继承我的代码:

<?php

// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;

// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);

// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}

$sql = "SELECT * FROM details WHERE Status = 0";

// Show data
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>First name</th>";
echo "<th>Last name</th>";
echo "<th>Destination</th>";
echo "<th>Value</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<form action='complete.php' method='post'>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Destination'] . "</td>";
echo "<td>" . $row['Status'] . "</td>";
echo "<td>" . "<input type='submit' value='Mark as complete'>" . "</td>";
echo "</tr>";
}
echo "</table>";
} 
else{
echo "No records matching your query were found.";
}
} 
else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

?>

和complete.php

<?php

// Declare Variables
$host = localhost;
$user = root;
$pass = root;
$db = test;

// Create connection to database
$link = mysqli_connect($host, $user, $pass, $db);

// Check to see if connection was established
if($link === false) {
die("Connection could not be established to database" .mysqli_error());
}

// sql to delete a record
$sql = "UPDATE details SET Status=1";

if ($link == true) {
echo "Marked as complete";
} else {
echo "Error updating record: " . $link->error;
}

mysqli_close($link)
?>

1 个答案:

答案 0 :(得分:0)

You are missing some steps. Here is the full process as you want to do.
Just replace with your. I hope this will solve your problem.

<?php

    // Declare Variables
    $host = localhost;
    $user = root;
    $pass = root;
    $db = test;

    // Create connection to database
    $link = mysqli_connect($host, $user, $pass, $db);

    // Check to see if connection was established
    if($link === false) {
    die("Connection could not be established to database" .mysqli_error());
    }

    $sql = "SELECT * FROM details WHERE Status = 0";

    // Show data
    if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
    echo "<table>";
    echo "<tr>";
    echo "<th>First name</th>";
    echo "<th>Last name</th>";
    echo "<th>Destination</th>";
    echo "<th>Value</th>";
    echo "</tr>";
    while($row = mysqli_fetch_array($result)){
    echo "<tr>";
    echo "<form action='complete.php' method='post'>";
    echo "<td>" . $row['FirstName'] . "</td>";
    echo "<td>" . $row['LastName'] . "</td>";
    echo "<td>" . $row['Destination'] . "</td>";
    echo "<td>" . $row['Status'] . "</td>";
    echo "<td>" . "<input type='submit' value='Mark as complete'>" . "</td>";
    // Put your primary key column name in the place of Id
    echo "<input type='hidden' name='user_id' value='".$row['Id']."'>"; 
    echo "</form>";
    echo "</tr>";
    }
    echo "</table>";
    } 
    else{
    echo "No records matching your query were found.";
    }
    } 
    else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }

    ?>

and complete.php


<?php

    // Declare Variables
    $host = localhost;
    $user = root;
    $pass = root;
    $db = test;

    // Create connection to database
    $link = mysqli_connect($host, $user, $pass, $db);

    // Check to see if connection was established
    if($link === false) {
    die("Connection could not be established to database" .mysqli_error());
    }

    // sql to delete a record
    // Put your primary key column name in the place of Id
    $sql = "UPDATE details SET Status=1 WHERE Id='".$_POST['user_id']."' "; 

    if ($link == true) {
    echo "Marked as complete";
    } else {
    echo "Error updating record: " . $link->error;
    }

    mysqli_close($link)
    ?>