gpt4 book ai didi

PHP 仅向 SQL 数据库添加一条新记录

转载 作者:行者123 更新时间:2023-11-29 22:40:31 24 4
gpt4 key购买 nike

我只需要将新数据添加到数据库表中。我正在尝试查询数据库,但仅添加新数据。我对 SQL 和 PHP 有点迷失了。

我已经将代码输入到数据库中,但刷新时它将再次输入相同的数据,我只希望它允许新数据:

<?php

// DATA BASE CONNECTION

DEFINE ('DB_USERS', 'root');
DEFINE ('DB_PASS', 'root');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'sam_api');

$dbc = @mysqli_connect (DB_HOST, DB_USERS, DB_PASS, DB_NAME) OR die ('could not connect' . mysqli_connect_error());


$user_update = $items['id'];



// Check if file already exists

$query = mysqli_query($con, "SELECT * FROM updates WHERE $user_update ='".$items['id']."'");

if(mysqli_num_rows($query) > 0){

echo "email already exists";
}else{
// do something
if (!mysqli_query($con,$query))
{
die('Error: ' . mysqli_error($con));
}
}


// now inserting the data form the previous page in to the database
$q = "INSERT INTO updates (user_update) VALUES ('$user_update')";
$r = @mysqli_query ($dbc,$q);



// telling you if it has been uploaded or not
if($r){
echo "you have successfully inserted";
} else{
echo "this did not work";
}


?>

最佳答案

从 user_update 中删除 $,因为您想要比较字段的值,而不是值与其本身。

 $query = mysqli_query($con, "SELECT * FROM updates WHERE user_update ='".$items['id']."'");

关于PHP 仅向 SQL 数据库添加一条新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29380158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com