2016-05-31 158 views
-1

我想插入數據到我的數據庫,但它不工作,我沒有得到任何錯誤。我猜想我的PHP代碼有些問題,但是我嘗試了一些網絡上的解決方案,這些解決方案適用於其他人,但不適合我。插入數據庫與PHP

我的html代碼:

<!Doctype HTML> 

<html> 

<title>Neues Projekt</title> 
<link rel="stylesheet" type="text/css" href="style.css"> 
<meta charset="UTF-8"> 

    <head> 
     <h1>Neues Projekt anlegen</h1> 
    </head> 

    <body> 
<form action="php_skript.php" method="post">  
    <p>Name des Projekts:</p> 
    <input type="text" name="name"> 
    <p>Ort des Projekts:</p> 
    <input type="text" name="ort"> 
    <p>Straße des Projekts:</p> 
    <input type="text" name="strasse"> 
    <p>Projektleiter:</p> 
    <input type="text" name="projektleiter"> 
    <br> 
    <input type="submit" value="Submit"> 
    </form> 
    Bilder hinzufügen: 
    <form method="post" enctype="multipart/form-data"> 
       <input type="file" name="image_data" multiple> 
      </form> 


     </form> 

    </body> 


</html> 

,在這裏我的PHP代碼:

<?php 
$user = 'root'; 
$password = 'root'; 
$db = 'projekte_db'; 
$host = 'localhost'; 
$port = 3306; 

$name=mysqli_real_escape_string($mysqli, $_POST['name']); 
$ort=mysqli_real_escape_string($mysqli, $_POST['ort']); 
$strasse=mysqli_real_escape_string($mysqli, $_POST['strasse']); 
$projektleiter=mysqli_real_escape_string($mysqli, $_POST['projektleiter']); 

$mysqli=new mysqli($host, $user, $password, $db); 


if ($mysqli->connect_errno) { 
    printf("Connect failed: %s\n", $mysqli->connect_error); 
    exit(); 
} 

$mysqli->query("INSERT INTO Projekt (name,ort,straße,projektleiter) VALUES ('$name','$ort','$strasse','$projektleiter')"); 

$mysqli->close(); 
?> 
+1

安置自己的數據庫架構。而且應該有錯誤。檢查服務器日誌 –

+0

'var_dump()'你的$ _POST變量,以確保它們被正確發送 – WillardSolutions

+1

第5個字符是特殊字符還是什麼? –

回答

1

$mysqli變量需要來之前:

$mysqli=new mysqli($host, $user, $password, $db); //put here 

$name=mysqli_real_escape_string($mysqli, $_POST['name']); 
$ort=mysqli_real_escape_string($mysqli, $_POST['ort']); 
$strasse=mysqli_real_escape_string($mysqli, $_POST['strasse']); 
$projektleiter=mysqli_real_escape_string($mysqli, $_POST['projektleiter']); 

//$mysqli=new mysqli($host, $user, $password, $db); remove here 

另外,我建議你使用參數化的查詢,它更安全。

+0

實際上現在它的工作原理,我想這個問題是因爲德語字母「ß」在「straße」中,而我的$ mysqli沒有出現在謝謝那個felippe和@Dr Manish Joshi – Korken55

1

既然你都將數據插入嘗試用準備好的發言

$user = 'root'; 
$password = 'root'; 
$db = 'projekte_db'; 
$host = 'localhost'; 

$mysqli = new mysqli($host, $user, $password, $db); 

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

$name=$_POST['name']; 
$ort=$_POST['ort']; 
$strasse=$_POST['strasse']; 
$projektleiter=$_POST['projektleiter']; 

/* create a prepared statement */ 
if ($stmt = $mysqli->prepare("INSERT INTO `Projekt` (`name`,`ort`,`straße`,`projektleiter`) VALUES (?,?,?,?)")) { 

    /* bind parameters for markers */ 
    /* use s for strings, i for integer,d for double, b for blob */ 
    $stmt->bind_param("ssss", $name,$ort,$strasse,$projektleiter); 

    /* execute query */ 
    $stmt->execute(); 

    /* close statement */ 
    $stmt->close(); 
} 
$mysqli->close(); 
+0

謝謝那個! – Korken55

+0

希望它幫助@ Korken55 –

+0

它實際上以前,但我想這個解決方案是一個更好,也許更安全的方式?去做吧。 @Peter Darmis – Korken55