2015-11-08 69 views
0

我連接到數據庫以加載html表單數據,並且在mysqli查詢中遇到了語法錯誤。我已經嘗試了語法上的變體,但它沒有清除。另一組眼睛將不勝感激。關於mysqli查詢的語法錯誤

<?php 
// define variables and set to empty values 
$ProjectErr = $ClientErr = $LastNameErr = $DateReceivedErr = ""; 
$Project = $Client = $LastName = $DateReceived = ""; 

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

    } 

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

    } 

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

    } 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["DateReceived"])) { 
    $DateReceivedErr = "The date the project was received is required"; 
    } else { 
    $DateReceived = test_input($_POST["DateReceived"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$DateReceived)) { 
     $DateReceivedErr = "Only letters and white space allowed"; 
    } 
    } 

    } 


function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
?> 

<?php 
$servername = "localhost"; 
$username = "oldga740_Tonymm"; 
$password = "JtAjDm#6"; 
$dbname = "oldga740_SeniorProject"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

//Sending form data to sql db. 
mysqli_query($connect,"INSERT INTO Projects (Project, Client, LastName, DateReceived) 
VALUES ('$_POST[post_Project]', '$_POST[post_Client]', '$_POST[post_LastName]', '$_POST[post_DateReceived]')"; 
?> 

<h2>New Project</h2> 
<p><span class="error">* required field.</span></p> 
<form action="send_post.php" method="post"> 
    Project: <input type="text" name="Project" value="<?php echo $Project;?>"> 
    <span class="error">* <?php echo $ProjectErr;?></span> 
    <br><br> 
    Client: <input type="text" name="Client" value="<?php echo $Client;?>"> 
    <span class="error">* <?php echo $ClientErr;?></span> 
    <br><br> 
    LastName: <input type="text" name="LastName" value="<?php echo $LastName;?>"> 
    <span class="error">* <?php echo $LastNameErr;?></span> 
    <br><br> 
    DateReceived: <input type="text" name="DateReceived" value="<?php echo $DateReceived;?>"> 
    <span class="error">* <?php echo $DateReceivedErr;?></span> 
    <br><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 

<?php 
echo "<h2>Your Input:</h2>"; 
echo $Project; 
echo "<br>"; 
echo $Client; 
echo "<br>"; 
echo $LastName; 
echo "<br>"; 
echo $DateReceived; 
?> 
+0

如果更改代碼以使用參數化查詢和執行,您將A)不會很容易受到SQL注入攻擊,並且B)更好地瞭解您實際執行的SQL是什麼。 – Michael

+0

感謝您的幫助Micheal – Tony

+0

順便說一句,您剛剛發佈了用於數據庫的用戶名和密碼,可能現在想改變它... –

回答

0

在您的代碼中有幾件事情是錯誤的或被認爲是編程世界中的不好實踐。而且,我幾乎無法指出每一個錯誤,所以我寫了一些更簡潔明瞭的代碼。我在我的機器上運行這個代碼,它工作得很好。

仔細閱讀代碼,這是很明顯的。

<?php 
function test_input($data){ 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "StackOverflow"; 

// create connection 
$connection = new mysqli($servername, $username, $password, $dbname); 

if(isset($_POST['submit']) && !$connection->connect_error){ 
    // to track errors 
    $error = false; 

    // now validate input fields 
    if (empty($_POST['Project']) || !isset($_POST['Project'])){ 
     $ProjectErr = "Project name is required"; 
     $error = true; 
    }elseif(!preg_match("/^[a-zA-Z\s]{1,}$/",$_POST['Project'])){ 
     // check if project only contains letters and whitespace 
     $ProjectErr = "Only letters and white space allowed"; 
     $error = true; 
    }else{ 
     $Project = test_input($_POST['Project']); 
    } 

    if (empty($_POST['Client']) || !isset($_POST['Client'])){ 
     $ClientErr = "Client name is required"; 
     $error = true; 
    }elseif(!preg_match("/^[a-zA-Z\s]{1,}$/",$_POST['Client'])){ 
     // check if client only contains letters and whitespace 
     $ClientErr = "Only letters and white space allowed"; 
     $error = true; 
    }else{ 
     $Client = test_input($_POST['Client']); 
    } 

    if (empty($_POST['LastName']) || !isset($_POST['LastName'])){ 
     $LastNameErr = "Last name is required"; 
     $error = true; 
    }elseif(!preg_match("/^[a-zA-Z\s]{1,}$/",$_POST['LastName'])){ 
     // check if last name only contains letters and whitespace 
     $LastNameErr = "Only letters and white space allowed"; 
     $error = true; 
    }else{ 
     $LastName = test_input($_POST['LastName']); 
    } 

    if (empty($_POST['DateReceived']) || !isset($_POST['DateReceived'])){ 
     $DateReceivedErr = "Data received field is required"; 
     $error = true; 
    }elseif(!preg_match("/^[a-zA-Z\s]{1,}$/",$_POST['DateReceived'])){ 
     // check if data received only contains letters and whitespace 
     $DateReceivedErr = "Only letters and white space allowed"; 
     $error = true; 
    }else{ 
     $DateReceived = test_input($_POST['DateReceived']); 
    } 

    if(!$error){ 
     $query = "INSERT INTO Projects (Project, Client, LastName, DateReceived) VALUES ('$Project', '$Client', '$LastName', '$DateReceived')"; 
     if($connection->query($query)){ 
      echo "record is successfully inserted!"; 
     }else{ 
      echo "error: record could not be inserted"; 
     } 
    } 
} 

?> 

<html> 
<head> 
    <title>Page Title</title> 
</head> 
<body> 
    <h2>New Project</h2> 
    <p><span class="error">* required field.</span></p> 
    <form action="send_post.php" method="post"> 
     Project: <input type="text" name="Project" value="<?php if(isset($Project)){ echo $Project; } ?>"> 
     <span class="error">* <?php if(isset($ProjectErr)){ echo $ProjectErr; } ?></span> 
     <br><br> 
     Client: <input type="text" name="Client" value="<?php if(isset($Client)){ echo $Client; } ?>"> 
     <span class="error">* <?php if(isset($ClientErr)){ echo $ClientErr; } ?></span> 
     <br><br> 
     LastName: <input type="text" name="LastName" value="<?php if(isset($LastName)){ echo $LastName; } ?>"> 
     <span class="error">* <?php if(isset($LastNameErr)){ echo $LastNameErr; } ?></span> 
     <br><br> 
     DateReceived: <input type="text" name="DateReceived" value="<?php if(isset($DateReceived)){ echo $DateReceived; } ?>"> 
     <span class="error">* <?php if(isset($DateReceivedErr)){ echo $DateReceivedErr; } ?></span> 
     <br><br> 
     <input type="submit" name="submit" value="Submit"> 
    </form> 

    <?php 
     echo "<h2>Your Input:</h2>"; 
     if(isset($_POST['Project'])){ echo $_POST['Project']; } 
     echo "<br />"; 
     if(isset($_POST['Client'])){ echo $_POST['Client']; } 
     echo "<br />"; 
     if(isset($_POST['LastName'])){ echo $_POST['LastName']; } 
     echo "<br />"; 
     if(isset($_POST['DateReceived'])){ echo $_POST['DateReceived']; } 
    ?> 
</body> 
</html> 
<?php 
    $connection->close(); 
?> 
+0

我很感激幫助。我希望你意識到我沒有要求任何人爲我寫這段代碼。我正在學習一些這方面的知識,但如果你能給我一個或兩個我的代碼需要嚴格調整的地方,我將不勝感激。我明顯可以將我的原始文件與您的修訂代碼進行比較,以查看修改內容的位置,但如果我不明白我做錯了什麼,我不會去了解。謝謝。 – Tony

+0

首先,您正在清理數據庫操作的輸入數據,但您沒有在插入查詢語句中使用任何這些數據。您正在創建一個mysqli對象_conn_,但您並未使用它來執行查詢,而是使用了程序方式。 –

+0

我感謝您的誠實反饋,並感謝您今天的幫助。我會確保下次更好地檢查我的代碼。只是好奇,是否有一個驗證器在任何地方檢查PHP? – Tony

0

我猜測它的這條線遇到了問題。

mysqli_query($connect," 
    INSERT INTO Projects (Project, Client, LastName, DateReceived) 
    VALUES ('" . $_POST["Project"] . "', '" . $_POST["Client"] . "', '" .$_POST["LastName"] . "', '" . $_POST["DateReceived"] . "');"); 

你的問題是兩個郵報「名」(它不是$_POST[post_Project]只是$_POST["Project"]),而且你剛纔寫他們SQL代碼,而不是將它們插入內。

+0

我認爲這可能是問題的一部分,所以我運行它的兩種方式,但我仍然得到語法錯誤,意外的';'在同一行上。 – Tony

+0

嘗試在末尾添加';'。順便說一句,我在'INSERT INTO Projects'行末尾丟失了一個')' –

+0

我剛剛意識到......我們在'PHP'語法結尾處缺少一個')','SQL沒什麼問題' –