问题更新数据库查询?

时间:2018-05-31 11:52:53

标签: php database mysqli

我是php的新手,也是数据库的新手。我面临更新数据库中的表的问题。我在输入字段中获取值但是当我更新查询时不起作用。

    $edit_id=$_GET['edit_id'];
    $get_data = mysqli_query($con, "SELECT * FROM userform WHERE id='$edit_id' ");
    $run = mysqli_fetch_array($get_data);
    if (!$run) {
        echo "something went wrong";
    }
    else{
        echo "Working Fine";
    }
    if(isset($_POST['update'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $subjects = $_POST['subjects'];
        $phone = $_POST['phone'];
        $country = $_POST['country'];
        $city = $_POST['city'];
        $address = $_POST['address'];
        $position = $_POST['position'];
        $about = $_POST['about'];
        $query = mysqli_query($con, "UPDATE userform SET name='$name', email='$email', subjects='$subjects', phone='$phone', country='$country', city='$city', address='$address', position='$position', about='$about', WHERE id='$edit_id' ");      
        //$run = mysqli_query($query);          
        if ($query) {
            echo "<script>alert('User Date Inserted Successfully'); </script>";
            //header('location:records.php');            
        }
        else{
            echo "<br> Something went wrong";
        }
        exit();
    }
?>
<div id="outer">
    <form method="POST" enctype="multipart/form-data">
        <input type="text" name="name" value="<?php echo $run['name']; ?>" >
        <input type="email" name="email" value="<?php echo $run['email']; ?>" >
        <input type="text" name="subjects" value="<?php echo $run['subjects']; ?>" >
        <input type="text" name="phone" value="<?php echo $run['phone']; ?>" >
        <input type="text" name="country" value="<?php echo $run['country']; ?>" >
        <input type="text" name="city" value="<?php echo $run['city'] ?>" >
        <input type="text" name="address" value="<?php echo $run['address']; ?>" >
        <input type="text" name="position" value="<?php echo $run['position']; ?>" >
        <textarea name="about" value="<?php echo $run['about']; ?>" ></textarea>
        <input id="sbmt" type="submit" value="Update Value" name="update">
    </form>

</div>

2 个答案:

答案 0 :(得分:0)

从更新查询中删除尾随的,(逗号)。在WHERE之前有一个逗号,让编译器认为应该有另一个需要更新的列。所以只需删除它。因此,请在$about之后删除

答案 1 :(得分:0)

首先,您必须在更新查询中删除,之后的'$about'(逗号)。它应该如下所示。

$query = mysqli_query($con, "UPDATE `userform` SET name='$name', email='$email', subjects='$subjects', phone='$phone', country='$country', city='$city', address='$address', position='$position', about='$about' WHERE id='$edit_id' "); 

还有一件事,如果您使用textarea作为输入,那么您应该将'替换为\'以更新mysql中的数据。因此,您的$run['about']应如下所示进行替换。

$run['about']=str_replace("'","\'",$run['about']);

首先进行此更改。