简体   繁体   中英

php Update function doesn't work

I'm trying to update row in mysql table with php, but for some reason it doesnt work.

I have validation and some checks on the variables and all of them works great, when I var_dump the variable in the php statement everything is great, so I guess the problem is in the query code to mysql. I will be really happy if you can help me!

This is the php part:

if (isset($_POST['editIdea'])) {
    $editIdea = trim($_POST['editIdea']);
    $editIdea = preg_replace('/[^А–Яа-яА-ЯA-Za-z0-9\. -!?,.@]/u', '', $editIdea);
    $editMoney = trim($_POST['editMoney']);
    $editMoney = preg_replace('/[^0-9\.]/', '', $editMoney);
    $editLong = trim($_POST['editLong']);
    $editLong = preg_replace('/[^А–Яа-яА-ЯA-Za-z0-9\. -!?,.@]/u', '', $editLong);
    $idnow = $_SESSION['id'];
    $error = false;

    if(mb_strlen($editIdea)<3 || mb_strlen($editIdea)>40) {
    echo '<b><p id="wrongname"> *blabla1 </b></p>';
    $error=true;
    }    

    if(mb_strlen($editLong)<9 || mb_strlen($editLong)>400) {
    echo '<b><p id="wronglongtext">*blabla2</b></p>';
    $error=true;
    }

    if($editMoney == false) {
    echo '<b><p id="wrongmoney"> *blabla3 </p> </b>';
    $error=true;
    }              

    if (!$error){
        var_dump($editIdea);
        var_dump($editMoney);
        var_dump($editLong);
       $sql = "UPDATE `Registration` SET `nameidea` = '$editIdea' , `moneyidea` = '$editMoney' , `explainidea`= '$editLong' WHERE `id` = '$idnow' ";
    }
else { 
       echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
}

So to say again my progress till now:

All var_dumps near the query statement are working great, variables have the information that I want to send. There are no errors in the php log. The statement doesn't go in the else, so I can't see any error or more information.

Also, i was trying with and without the "`" near the rows and database names.

Where is my mystake in the query syntax?

Thanks in advance again!

**All the names of the database and rows are correct and double checked.

Add this code after $sql = ....

$link = mysqli_connect("localhost", "my_user", "my_password", "db_name");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if (!mysqli_query($link, $sql)) {
    printf("Error message: %s\n", mysqli_error($link));
}

/* close connection */
mysqli_close($link);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM