无法使用复合主键将数据保存到新表

时间:2016-06-29 09:52:05

标签: php mysql wamp

代码

require_once 'detect/detect.php';
date_default_timezone_set('Asia/Kuala_Lumpur');

$fb_id = $_POST['id'];
$full_name = $_POST['name'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$date_time = date('Y-m-d H:i:s');
$shared = null;
$redeemed = null;

$connection = mysqli_connect("server", "username", "password", "db");

$sql = "INSERT IGNORE INTO ABC_CAFE (fb_id, full_name, first_name, last_name, email, date_time, shared, redeemed) VALUES ('$fb_id','$full_name','$first_name','$last_name','$email','$date_time','$shared','$redeemed')";

mysqli_close($connection);

我的表创建时没有数据。 主键是(fb_id,date_time),它没有出现在结构中。

2 个答案:

答案 0 :(得分:2)

干净的方式:

$mysqli = new mysqli("server", "username", "password", "db");

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

$stmt = $mysqli->prepare("INSERT IGNORE INTO ABC_CAFE (fb_id, full_name, first_name, last_name, email, date_time, shared, redeemed)  VALUES (?, ?, ?, ?, ?, ?, ?, ?)");

$stmt->bind_param('isssssss', $fb_id, $full_name, $first_name, $last_name, $email, $date_time, $shared, $redeemed);

/* execute prepared statement */
$stmt->execute();

$mysqli->close();

答案 1 :(得分:0)

尝试使用mysqli_query()来执行查询。

试试这个。

$connection = mysqli_connect("server", "username", "password", "db");
$sql = "INSERT IGNORE INTO ABC_CAFE (fb_id, full_name, first_name, last_name, email, date_time, shared, redeemed) VALUES ('$fb_id','$full_name','$first_name','$last_name','$email','$date_time','$shared','$redeemed')";
mysqli_query($connection, $sql);
mysqli_close($connection);