无法从PHP更新mysql列

时间:2017-03-23 10:30:00

标签: php mysql

我正在尝试使用此

更新数据库表的列
$url = mysqli_connect($servername, $dbusername, $usrpassword, $dbname);
// Check connection
if (!$url) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT adv_val FROM current_advert";

if($result = mysqli_query($url, $sql)){

        while($row = mysqli_fetch_assoc($result)){

                $adv_val = $row['adv_val'];        
        }
} 
echo "current advert is:" . $adv_val;

// Attempt select query execution

$sql = "SELECT advert_text FROM advertisements WHERE advert_id = $adv_val";

if($result = mysqli_query($url, $sql)){

        while($row = mysqli_fetch_assoc($result)){

                $advert = $row['advert_text'];        
        }
}
echo "<br>current advert text is:" . $advert;


if ($adv_val == 1 OR $adv_val == 2 OR $adv_val == 3) {
    $adv_val = $adv_val + 1;
} else {
    $adv_val = 1;
}

$sql = "UPDATE current_advert SET adv_val='$adv_val'";
// Close connection
echo "<br>next advert id is:" . $adv_val;
mysqli_close($url);

与数据库的连接是可以的,因为我能够在脚本的开头读取数据。这让我很伤心!

1 个答案:

答案 0 :(得分:1)

    $sql = "UPDATE current_advert SET adv_val='$adv_val'";
 mysqli_query($url, $sql);

你错过了第二行。