2017-06-22 66 views
1

我試圖從簡單的註冊表格中插入數據到MySQL中。我有我的數據庫託管在亞馬遜AWS RDS上,使用DBeaver編輯它。當我運行代碼時,我得到以下內容無法弄清楚爲什麼我的PHP沒有插入到MySQL DB中

affected_rows。「插入到數據庫中的數據」。 } else {echo「發生了 錯誤,未添加項目。」; } $ db-> close(); ?>

我該如何解決這個問題?我的PHP錯了嗎?我很疑惑我是否可以使用MySQLi,我將如何確定?我假設Amazon RDS MySQL兼容。

<?php 
// create short variable names 
$name=$_POST['name']; 
$birthdate=$_POST['birthdate']; 
$email=$_POST['email']; 
$password=$_POST['password']; 
$address=$_POST['address']; 
$city+$_POST['city']; 

if (!get_magic_quotes_gpc()) { 
    $name = addcslashes($name); 
    $birthdate = addslashes($birthdate); 
    $email = addcslashes($email); 
    $password = addcslashes($password); 
    $address = addcslashes($address); 
    $city = addcslashes($city); 
} 

$host='xxxxxx' 
$user='admin' 
$password='xxxxx' 
$dbname='users' 
@ $db = mysqli_connect($host,$user,$password,$dbname) 

if (mysqli_connect_errno()) { 
    echo 'Error: Could not connect to database. Please try again later.'; 
    exit; 
} 

// Execute the query 
$query = "INSERT INTO vestorinfo (name,birthdate,email,password,address,city) 
    VALUES ('$_POST[name]','$_POST[birthdate]','$_POST[email]','$_POST[password]','$_POST[address]','$_POST[city]')"; 
$result = mysqli_query($query) 
     or die ("Couldn't execute query."}; 

if ($result) { 
    echo $db->affected_rows." data inserted into database."; 
} else { 
    echo "An error has occurred. The items were not added."; 
} 

$db->close(); 
?> 

這裏是從HTML頁面的形式

<div id="registerform"> 
    <form action="php/registerprocess.php" method="post" class="form-horizontal"> 

<fieldset> 

<!-- Form Name --> 

<!-- Text input--> 
<div class="form-group"> 
    <label class="col-md-4 control-label" for="name">Name</label> 
    <div class="col-md-4"> 
    <input id="name" name="name" placeholder="Your name" class="form-control input-md" required="" type="text"> 

    </div> 
</div> 

<!-- Text input--> 
<div class="form-group"> 
    <label class="col-md-4 control-label" for="birthdate">Birth Date</label> 
    <div class="col-md-4"> 
    <input id="birthdate" name="birthdate" placeholder="07/04/1950" class="form-control input-md" required="" type="text"> 

    </div> 
</div> 

<!-- Text input--> 
<div class="form-group"> 
    <label class="col-md-4 control-label" for="email">Email</label> 
    <div class="col-md-4"> 
    <input id="email" name="email" placeholder="" class="form-control input-md" required="" type="text"> 

    </div> 
</div> 

<!-- Password input--> 
<div class="form-group"> 
    <label class="col-md-4 control-label" for="password">Password</label> 
    <div class="col-md-4"> 
    <input id="password" name="password" placeholder="" class="form-control input-md" required="" type="password"> 
    <span class="help-block">Must be &gt;= 8 characters including at least 1 number</span> 
    </div> 
</div> 

<!-- Text input--> 
<div class="form-group"> 
    <label class="col-md-4 control-label" for="email">Address</label> 
    <div class="col-md-4"> 
    <input id="address" name="address" placeholder="" class="form-control input-md" required="" type="text"> 
    </div> 
</div> 

<!-- Text input--> 
<div class="form-group"> 
    <label class="col-md-4 control-label" for="email">City</label> 
    <div class="col-md-4"> 
    <input id="city" name="city" placeholder="" class="form-control input-md" required="" type="text"> 
    </div> 
</div> 
</div> 

</fieldset> 
+3

它看起來像你的PHP沒有運行,爲什麼不回顯真正的錯誤? –

+1

您已經開放SQL注入。由於您使用的是mysqli,請利用[prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和[bind_param](http://php.net/手動/ EN/mysqli的-stmt.bind-param.php)。這將解決您可能遇到的任何煩人的引用問題。同時檢查一路上的mysqli錯誤。 – aynber

+0

'$ city + $ _ POST ['city'];'應該是'$ city = $ _ POST ['city'];'在設置所有這些值之後,爲什麼要回退到在插入中使用$ _POST值? –

回答

1

你的代碼是在SQL注入風險,你應該使用PARAM結合,而不是var當成$ _ POST 反正尊重你質疑失蹤插入值 可能因爲涉及到你指的$ _ POST指數在錯誤的方法其實

例如:使用級聯,你應該

$query = "INSERT INTO vestorinfo (name,birthdate,email,password,address,city) 
     VALUES ('" . $_POST['name'] ." , " . $_POST['birthdate'] .", " . 
     $_POST['email'] . "," . $_POST['password'] . "," . 
     $_POST['address'] . "," . $_POST['city'] . ")"; 
+0

雖然還有很多其他的問題,比如PHP甚至沒有被執行。 – chris85

+0

@ chris85。可能 。我只是第一次瞥見。 – scaisEdge

+0

我想在我開始處理注入問題之前至少得到PHP執行 – PhilFredo

相關問題