如何从MySQL中检索特定值(PHP)

时间:2013-10-21 05:26:13

标签: php mysql forms get sql-update

我是PHP的新手,我想在这个小脚本上输入一些内容。一切似乎都有效但我需要知道如何检索被修改的消息的“id”。

<?php
function updatemyinfos() {
$con=mysqli_connect("localhost","root","root","dbname");
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
**$id=**
$sql="UPDATE table1 SET message = '$_POST[message]' WHERE id ='$id'";

if (!mysqli_query($con,$sql))
{
    die('Error: ' . mysqli_error($con));
}
echo '<p>Message modifié</p>';
mysqli_close($con);
}

if(isset($_POST['updatemessage']))
{
    updatemyinfos();
}
?>
<form action="#" method="post">
<textarea style="resize:none" cols="1" rows="1" name="message"id="textbox">'.$row['message'].'</textarea>           
<input type="submit" value="Soumettre" name="updatemessage" id="updatemessage">
</form>

编辑:我试过这个..我没有收到任何错误消息但是数据库没有更新。但我得到了“回声”

消息修改

<?php
function updatemyinfos($id) {
$con=mysqli_connect("localhost","root","root","TP1AlexandreBouletCouture");
                if (mysqli_connect_errno())
                  {
                  echo "Failed to connect to MySQL: " . mysqli_connect_error();
                  }



                $sql="UPDATE table1 SET message = '$_POST[message]' WHERE id ='$id'";




                if (!mysqli_query($con,$sql))
                  {
                  die('Error: ' . mysqli_error($con));
                  }
                echo '<p>Message modifié</p>';


                mysqli_close($con);
            }

            if(isset($_POST['updatemessage']))
            {
            updatemyinfos($_POST['updatemessage']);
            }
            ?>

2 个答案:

答案 0 :(得分:0)

更改这些行

if(isset($_POST['updatemessage']))
{
updatemyinfos();
}

if(isset($_POST['updatemessage']))
{
updatemyinfos($_POST['updatemessage']);
}

和这个

function updatemyinfos()

function updatemyinfos($id)

答案 1 :(得分:0)

您好,请尝试以下查询,

    $sql1 = "Select id from table where message = '$_POST[message]'";