试图制作将阿拉伯数据插入mysql的代码

时间:2017-07-25 09:36:35

标签: php mysql

我按下html表单中的按钮时会运行以下代码

<!DOCTYPE html>
<html>
<head>
<?php $timestamp = date("YmdHis"); ?>
<link rel="stylesheet" type="text/css" href="style.css"?v=<?php echo time(); ?>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Data Entry</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql->set_charset('utf8');
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS mortgagedb";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully";
} else {
    echo "Error creating database: " . $conn->error;
}


$sql = "CREATE TABLE IF NOT EXISTS mortgagedb . clientstest (
reg_date TIMESTAMP,
client_code INT(6) UNSIGNED PRIMARY KEY,
project_code INT(6) UNSIGNED NOT NULL,
client_name VARCHAR(255) NOT NULL,
client_id INT(14) NOT NULL,
client_id_expiry VARCHAR(7) NOT NULL,
client_address VARCHAR(255) NOT NULL,
client_profession VARCHAR(255) NOT NULL,
client_phone INT(12) NOT NULL,
income INT(6) NOT NULL,
guarantor_name VARCHAR(255) NOT NULL,
guarantor_id VARCHAR(14) NOT NULL,
guarantor_id_expiry VARCHAR(7) NOT NULL,
guarantor_address VARCHAR(255) NOT NULL,
guarantor_relation VARCHAR(255) NOT NULL,
unit_no VARCHAR(255) NOT NULL,
floor_no VARCHAR(255) NOT NULL,
bulding_no VARCHAR(255) NOT NULL,
location VARCHAR(255) NOT NULL,
project VARCHAR(255) NOT NULL,
city VARCHAR(255) NOT NULL,
governorate VARCHAR(255) NOT NULL,
area VARCHAR(255) NOT NULL,
unit_value INT(7) NOT NULL,
previous_payment INT(7) NOT NULL,
loan_maturity INT(2) NOT NULL,
mortgage_rate INT(2) NOT NULL,
annual_rate_wage INT(2) NOT NULL,
max_pay_inc INT(2) NOT NULL,
prog_max_inc INT(7) NOT NULL,
prog_min_inc INT(7) NOT NULL,
prog_max_sub INT(7) NOT NULL,
prog_min_sub INT(7) NOT NULL,
payment_graduation INT(2) NOT NULL,
iscore VARCHAR(255) NOT NULL,
documents VARCHAR(255) NOT NULL,
condition1 VARCHAR(255) NOT NULL,
condition2 VARCHAR(255) NOT NULL,
condition3 VARCHAR(255) NOT NULL,
condition4 VARCHAR(255) NOT NULL,
condition5 VARCHAR(255) NOT NULL,
condition6 VARCHAR(255) NOT NULL,
condition7 VARCHAR(255) NOT NULL,
condition8 VARCHAR(255) NOT NULL,
condition9 VARCHAR(255) NOT NULL,
condition10 VARCHAR(255) NOT NULL,
condition11 VARCHAR(255) NOT NULL,
condition12 VARCHAR(255) NOT NULL,
condition13 VARCHAR(255) NOT NULL,
condition14 VARCHAR(255) NOT NULL,
attachment1 VARCHAR(255) NOT NULL,
attachment2 VARCHAR(255) NOT NULL,
attachment3 VARCHAR(255) NOT NULL,
attachment4 VARCHAR(255) NOT NULL,
attachment5 VARCHAR(255) NOT NULL,
attachment6 VARCHAR(255) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
    echo "Table clientstest created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}
$sql = "INSERT INTO mortgagedb . clientstest (client_code, project_code, client_name, client_id, client_id_expiry, client_address, client_profession, client_phone, income, guarantor_name, guarantor_id, guarantor_id_expiry, guarantor_address, guarantor_relation, unit_no, floor_no, bulding_no, location, project, city, governorate, area, unit_value, previous_payment, loan_maturity, mortgage_rate, annual_rate_wage, max_pay_inc, prog_max_inc, prog_min_inc, prog_max_sub, prog_min_sub, payment_graduation, iscore, documents, condition1, condition2, condition3, condition4, condition5, condition6, condition7, condition8, condition9, condition10, condition11, condition12, condition13, condition14, attachment1, attachment2, attachment3, attachment4, attachment5, attachment6)
VALUES ('$_POST[ccode]', '$_POST[pcode]', '$_POST[cname]', '$_POST[cid]', '$_POST[ciddate]', '$_POST[caddress]', '$_POST[cjob]', '$_POST[cphone]', '$_POST[inc]', '$_POST[gname]', '$_POST[gid]', '$_POST[giddate]', '$_POST[gaddress]', '$_POST[grel]', '$_POST[un]', '$_POST[floor]', '$_POST[bn]', '$_POST[lctn]', '$_POST[pro]', '$_POST[city]', '$_POST[gov]', '$_POST[area]', '$_POST[unitv]', '$_POST[pp]', '$_POST[lm]', '$_POST[mr]', '$_POST[row]', '$_POST[paytoinc]', '$_POST[maxinc]', '$_POST[mininc]', '$_POST[maxsub]', '$_POST[minsub]', '$_POST[pgr]', '$_POST[iscore]', '$_POST[docs]', '$_POST[con1]', '$_POST[con2]', '$_POST[con3]', '$_POST[con4]', '$_POST[con5]', '$_POST[con6]', '$_POST[con7]', '$_POST[con8]', '$_POST[con9]', '$_POST[con10]', '$_POST[con11]', '$_POST[con12]', '$_POST[con13]', '$_POST[con14]', '$_POST[att1]', '$_POST[att2]', '$_POST[att3]', '$_POST[att4]', '$_POST[att5]', '$_POST[att6]' )";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
</body>
</html>

但是我收到以下错误

  

致命错误:未捕获错误:调用成员函数set_charset()   在C:\ xampp \ htdocs \ Mortgage Project \ db.php中为null:21堆栈跟踪:#0   在第21行的C:\ xampp \ htdocs \ Mortgage Project \ db.php中抛出{main}

我知道错误是关于set_char,但我是新手,并且在此处并不真正理解这个问题,提前感谢。

2 个答案:

答案 0 :(得分:1)

您的错误来自这一行:

$sql->set_charset('utf8');

基本上是正确的,但你还没有定义$sql。您需要在连接对象上使用set_charset

所以代替你的代码,写下:

$conn->set_charset('utf8');

答案 1 :(得分:0)

你有$sql->set_charset('utf8');。然而。 $sql仅在下一行定义!

尝试将其更改为$conn