2016-03-27 49 views
0

大家好,你可以看到我已經把因爲 我嘗試連接和信息寫入到數據庫中是非常基本和簡單的整個代碼,但我不明白我我做錯了嗎? 我想讓用戶通過填寫​​表單並在用戶給出他的信息後將信息放入數據庫中,我想將它放入數據庫中。數據庫本身是生成的,並且生成表和行。無法連接,並寫入mysql數據庫

<!DOCTYPE HTML> 
<html> 
<head> 
<style> 
.error {color: #FF0000;} 
</style> 
</head> 
<body> 

<?php 
// define variables and set to empty values 
$nameErr = $emailErr = $genderErr = $phoneErr = $cityErr = $countyErr = $adressErr = ""; 
$name = $email = $gender = $phone = $instagram = $facebook = $city = $country = $adress = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["name1"])) { 
    $nameErr = "Name is required"; 
    } else { 
    $name = test_input($_POST["name1"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) { 
     $nameErr = "Only letters and white space allowed"; 
    } 
    } 

    if (empty($_POST["email"])) { 
    $emailErr = "Email is required"; 
    } else { 
    $email = test_input($_POST["email"]); 
    // check if e-mail address is well-formed 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     $emailErr = "Invalid email format"; 
    } 
    } 

    if (empty($_POST["phone"])) { 
    $phone = "Phone number is required"; 
    } else { 
     $phone = test_input($_POST["phone"]); 
    } 

    if (empty($_POST["gender"])) { 
    $genderErr = "Gender is required"; 
    } else { 
    $gender = test_input($_POST["gender"]); 
    } 
} 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
?> 
<h2>Amsterdam event attending</h2> 
<p><span class="error">* required field.</span></p> 
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>, <"> 
    Name: 
    <input type="text" name="name1" value="<?php echo $name;?>"> 
    <span class="error">* <?php echo $nameErr;?></span> 
    <br><br> 
    Adress: 
    <input type="text" name="adress" value="<?php echo $adress;?>"> 
    <span class="error">*</span> 
    <br><br> 
    City: 
    <input type="text" name="city" value="<?php echo $city;?>"> 
    <span class="error">*</span> 
    <br><br> 
    E-mail: 
    <input type="text" name="email" value="<?php echo $email;?>"> 
    <span class="error">* <?php echo $emailErr;?></span> 
    <br><br> 
    Instagram: 
    <input type="text" name="insta" value=""> 
    <br><br> 
    Facebook: 
    <input type="text" name="face" value=""> 
    <br><br> 
    Phone number: 
    <input type="text" name="phone" value="<?php echo $phone;?>"> 
    <span class="error">* <?php echo $phoneErr;?></span> 
    <br><br> 
    Gender: 
    <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female 
    <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male 
    <span class="error">* <?php echo $genderErr;?></span> 
    <br><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 

<?php 
if(isset($_POST['submit'])){ 
} 
$servername = 'localhost'; 
$username = 'root'; 
$password = '1234'; 
$database = 'amsterdam'; 

$conn = mysqli_connect($servername, $username, $password, $database); 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_errno());   
    } 
    $sql = "INSERT INTO inschrijvingen (name, adress, city, email, insta, face, phone, gender); 
    VALUES ($_POST[name1], $_POST[adress], $_POST[city], $_POST[email], $_POST[insta], $_POST[face], $_POST[phone], $_POST[gender])"; 
    mysql_close($conn); 
    ?> 

</body> 
</html> 
+1

是什麼error_log中說什麼? – Stuart

回答

1

你應該把你的VALUES放在單引號中。但是,當你在查詢中直接寫您的$_POST變量,因爲周圍的$_POST指數名稱的單引號在你的代碼將創建錯誤,這是不可能的。你正在做的主要錯誤是將你的$_POST變量直接傳遞到你的查詢中,這將造成嚴重的安全問題,如SQL注入。 您應該使用mysqli_real_escape_string()函數來完成此操作,併爲$ _POST數組的每個索引創建變量。

這是我應該怎樣做:

$conn = mysqli_connect($servername, $username, $password, $database); 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_errno());   
    } 
$name=mysqli_real_escape_string($_POST['name1']); 
$adress=mysqli_real_escape_string($_POST['adress']); 
$city=mysqli_real_escape_string($_POST['city']); 
$email=mysqli_real_escape_string($_POST['email']); 
$insta=mysqli_real_escape_string($_POST['insta']); 
$face=mysqli_real_escape_string($_POST['face']); 
$phone=mysqli_real_escape_string($_POST['phone']); 
$gender=mysqli_real_escape_string($_POST['gender']); 

    $sql = "INSERT INTO inschrijvingen (name, adress, city, email, insta, face, phone, gender); 
     VALUES ('$name', '$adress', '$city', '$email', '$insta', '$face', '$phone', '$gender')"; 

$result=mysqli_query($conn,$sql); 

我沒有審查代碼之前和之後的代碼。我只是糾正了查詢和值參數。我還將mysqli_query()函數添加到您的代碼中。這個函數是主要的功能,它負責執行你在代碼中錯過的查詢。讓我知道這是否能解決您的問題。

+0

* benissimo * ;-) –

+0

@弗雷德-II-;!0)謝謝 – DevMan

+0

帕戈(SI,parlo拉vostra通用未PO);-) –

0

我認爲問題在於你試圖打開一個I/O流(與SQL服務器的流)後寫在頁面上的東西。 您無法打開標準輸出(流到您正在生成的頁面),然後嘗試在php中打開另一個流。在將第一個字符寫入頁面之前,需要生成所有其他流。試試這個:

<?php 
// define variables and set to empty values 
$nameErr = $emailErr = $genderErr = $phoneErr = $cityErr = $countyErr = $adressErr = ""; 
$name = $email = $gender = $phone = $instagram = $facebook = $city = $country = $adress = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["name1"])) { 
    $nameErr = "Name is required"; 
    } else { 
    $name = test_input($_POST["name1"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) { 
     $nameErr = "Only letters and white space allowed"; 
    } 
    } 

    if (empty($_POST["email"])) { 
    $emailErr = "Email is required"; 
    } else { 
    $email = test_input($_POST["email"]); 
    // check if e-mail address is well-formed 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     $emailErr = "Invalid email format"; 
    } 
    } 

    if (empty($_POST["phone"])) { 
    $phone = "Phone number is required"; 
    } else { 
     $phone = test_input($_POST["phone"]); 
    } 

    if (empty($_POST["gender"])) { 
    $genderErr = "Gender is required"; 
    } else { 
    $gender = test_input($_POST["gender"]); 
    } 
} 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
?> 
    <!DOCTYPE HTML> 
<html> 
<head> 
<style> 
.error {color: #FF0000;} 
</style> 
</head> 
<body> 
<h2>Amsterdam event attending</h2> 
<p><span class="error">* required field.</span></p> 
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>, <"> 
    Name: 
    <input type="text" name="name1" value="<?php echo $name;?>"> 
    <span class="error">* <?php echo $nameErr;?></span> 
    <br><br> 
    Adress: 
    <input type="text" name="adress" value="<?php echo $adress;?>"> 
    <span class="error">*</span> 
    <br><br> 
    City: 
    <input type="text" name="city" value="<?php echo $city;?>"> 
    <span class="error">*</span> 
    <br><br> 
    E-mail: 
    <input type="text" name="email" value="<?php echo $email;?>"> 
    <span class="error">* <?php echo $emailErr;?></span> 
    <br><br> 
    Instagram: 
    <input type="text" name="insta" value=""> 
    <br><br> 
    Facebook: 
    <input type="text" name="face" value=""> 
    <br><br> 
    Phone number: 
    <input type="text" name="phone" value="<?php echo $phone;?>"> 
    <span class="error">* <?php echo $phoneErr;?></span> 
    <br><br> 
    Gender: 
    <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female 
    <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male 
    <span class="error">* <?php echo $genderErr;?></span> 
    <br><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 

<?php 
if(isset($_POST['submit'])){ 
} 
$servername = 'localhost'; 
$username = 'root'; 
$password = '1234'; 
$database = 'amsterdam'; 

$conn = mysqli_connect($servername, $username, $password, $database); 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_errno());   
    } 
    $sql = "INSERT INTO inschrijvingen (name, adress, city, email, insta, face, phone, gender); 
    VALUES ($_POST[name1], $_POST[adress], $_POST[city], $_POST[email], $_POST[insta], $_POST[face], $_POST[phone], $_POST[gender])"; 
    mysql_close($conn); 
    ?> 

</body> 
</html> 
+0

非常感謝你們,所有你們所說的就是我所缺少的東西我實施了你的解決方案,它迄今爲止效果非常好! –

0

有幾件事情,我注意到:

您有沒有使用

if(isset($_POST['submit'])){ 
} 

$ SQL定義中途有一個分號通過它的if語句,也許應該是:

$sql = "INSERT INTO inschrijvingen (name, adress, city, email, insta, face, phone, gender) 
VALUES ($_POST[name1], $_POST[adress], $_POST[city], $_POST[email], $_POST[insta], $_POST[face], $_POST[phone], $_POST[gender])"; 

定義$ sql後,您從未實際查詢過數據庫

mysqli_query($conn, $sql); 

最後,你有混合mysql和mysqli的。你接近的說法應該是

mysqli_close($conn); 

還有一些其他問題,以及,但它看起來好像一些其他的答案都已經解決了這些。

0

你需要把你的代碼放在你的if(isset($_POST['submit']))大括號中,並把你的SQL放到else括號中。另外,似乎你正在使用mysql_close()而不是mysqli_close()

if(isset($_POST['submit'])){ 

$servername = 'localhost'; 
$username = 'root'; 
$password = '1234'; 
$database = 'amsterdam'; 

$conn = mysqli_connect($servername, $username, $password, $database); 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_errno());   
    }else{ 

    $name=$_POST['name1']; 
    $adress=$_POST['adress']; 
    $city=$_POST['city']; 
    $email=$_POST['email']; 
    $insta=$_POST['insta']; 
    $face=$_POST['face']; 
    $phone=$_POST['phone']; 
    $gender=$_POST['gender']; 

    $sql = "INSERT INTO inschrijvingen (name, adress, city, email, insta, face, phone, gender) 
    VALUES ('$name', '$adress','$city', '$insta, '$face', '$phone', '$gender')"; 
    mysqli_close($conn); 
    } 
} 

使用準備好的陳述。

您的代碼可以很容易地注入。我會建議使用準備好的語句。

在這種情況下,你準備好的聲明看起來像:

$sql = "INSERT INTO inschrijvingen (name, adress, city, email, insta, face, phone, gender) 
VALUES (?,?,?,?,?,?)"; 

$stmt = $conn->prepare($sql); 
$stmt->bind_param("sssssss", $name, $adress, $city, $email, $insta, $face, $phone, $gender); 
$stmt->close(); 
$conn->close();