我試圖向mysql表中添加一個新的用戶名throw wordpress。但每次我嘗試這樣做時,我都沒有錯誤消息,但是沒有行添加到數據庫中。不使用wordpress和php在mysql數據庫中輸入數據
這是裏面的PHP WordPress的頁面:
<table>
<form name="form1" method="post" action="">
<strong>Please enter your information in order to download the Macs Cabs
App</strong>
<tr><td>
Name:</td><td><input name="Name" type="text" id="sName"></td></tr>
Email Address:</td><td><input name="Email" type="text" id="sEmail"></td> </tr>
<tr><td>
<input type="submit" name="Submit" value="Submit"></td></tr>
</form></table>
<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("foundint_Sababa", $con);
$sql="INSERT INTO `Users` (`sName`, `sEmail`)
VALUES ('{$_POST['sName']}','{$_POST['sEmail']}')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
$info = mysql_info(); echo $info;
mysql_close($con);
?>
我看不出什麼錯。我認爲這可能是連接有問題。有任何想法嗎?
謝謝!
編輯
至於建議,我試着用$wpdb
所以我創建一個PHP文件的文件夾在叫我的碼(在可溼性粉劑管理員,可溼性粉劑內容和WP-同級別包括),我下面的代碼添加到文件調用insertUser.php:
<?php
global $wpdb;
$wpdb->insert("wp_submitted_form", array(
"sName" => $sName,
"sEmail" => $sEmail));
?>
現在,在我的網頁我試着去調用這個函數和進出口這樣做:
<table>
<form name="form1" method="post" action="">
<strong>Please enter your information in order to download the Macs Cabs
App</strong>
<tr><td>
Name:</td><td><input name="Name" type="text" id="sName"></td></tr>
Email Address:</td><td><input name="Email" type="text" id="sEmail"></td> </tr>
<tr><td>
<input type="submit" name="Submit" value="Submit"></td></tr>
</form></table>
<?php
if(isset($_POST['Submit']))
{
include("./my-codes/insertUsers.php");
}
?>
而我仍然無法在數據庫中插入任何行。有什麼建議麼?
編輯 我需要一個插件來實際連接我的sql數據庫和wordpress。代碼是正確的。
注意:'mysql_ *'函數已被棄用,它們已從PHP 7中刪除,當您升級到該版本時,您的代碼將停止工作。您不應使用它們編寫新代碼,而應使用['mysqli_ *'或PDO](http://php.net/manual/en/mysqlinfo.api.choosing.php)。 –
這是一個WordPress的頁面?有沒有WordPress的功能... – Lee
你應該使用'$ wpdb'在Wordpress中進行數據庫查詢 – Lee