2015-12-16 68 views
1

這是我的第一篇文章。請溫柔。列名顯示在查詢結果中mysql php

我正在爲設備服務歷史記錄創建一個基本數據庫。我最初使用MySQL。但是,在閱讀了許多帖子後,我試圖改變爲mysqli。我的頁面中的前兩個數據庫連接正常工作。但第三個在我的服務歷史部分發布「0000-00-00 $ tech $ history」。當我使用MySQL vs mysqli時,一切正常。我確定我錯過了一些簡單的東西。我搜索谷歌,找不到任何東西來解決這個問題。

這是給我的問題的頁面的PHP部分。先謝謝你。

//This section sends service data to the specified database// 
$con = mysqli_connect("localhost","root","pass","bed_logs"); 

// Check connection 
if (mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

if(isset($_POST['add'])) 
    if(! get_magic_quotes_gpc()) 
    { 
     $history = addslashes ($_POST['history']); 
     $service_date = addslashes ($_POST['service_date']); 
     $tech = addslashes ($_POST['tech']); 

    } 
    else 
    { 
     $tag5156 = $_POST['history']['tech']['service_date']; 
    } 
$sql = 'INSERT INTO tag5156 (service_date,tech,history) VALUES ("$service_date","$tech","$history")'; 

while ($row = mysqli_fetch_assoc($retval)) 
{ 
    print_r ($row); 
} 
$retval = mysqli_query($con,$sql); 

if(! $retval) 
{ 
    die('Could not enter data: ' . mysqli_error($con)); 
} 
{ 
    echo "<CENTER>Record Updated</CENTER><BR><BR>"; 
} 
mysqli_close($con); 

HTML似乎工作正常。

<!--This is the submit form--> 

    <form action="<?php $_PHP_SELF ?>" method="post"> 
<div> 
    <td style="width: 200px; height: 28px;"> 
<table style="width: 100%"> 
<tr> 
<!--SERVICE DATE--> 
    <td style="height: 45px" class="auto-style2"><strong>Service  Date:</strong></td> 
    <td style="height: 45px; width: 132px;"> 
    <input id="service_date" name="service_date" value="<?php echo date('Y- m-d'); ?>"</td style="width: 80px"> 
<!--TECHNICIAN NAME-->  
    <td style="height: 45px" class="auto-style2"> 
    <strong>Tech:</strong></td> 
    <td style="height: 45px; width: 165px;"> 
    <input id="tech" name="tech" type="text"></td> 
<!--WORK PERFORMED--> 
    <td style="height: 45px" class="auto-style2"><strong>Work 
Performed:</strong></td> 
    <td style="height: 45px"> 
    <input id="history" name="history" style="width: 750px; height: 25px" 
type="text"></td> 
</tr> 
</table> 
<!--BUTTON TO UPDATE SERVICE RECORD--> 
<input id="add" name="add" type="submit" value="Update Service Record" 
onClick="return empty()">&nbsp; 
+0

當您在字符串中使用PHP變量時,如果在雙引號內而不是單引號內,它們將展開爲它們的值。試着在'while循環之前'echo'你的'$ sql'變量來看看它的樣子。 –

+0

爲了可讀性,您需要以一致的方式縮進代碼 - 不僅僅是爲了美觀,還因爲它有助於顯示塊嵌套的潛在問題。而且,由於多種原因,將'INSERT'放入準備好的語句中,您將會更加領先。 –

+0

我一定在某個地方看不到我的錯。我開始了,它正在工作。 – MisterB

回答

0

這條線:

$sql = 'INSERT INTO tag5156 (service_date,tech,history) VALUES ("$service_date","$tech","$history")'; 

應該要麼

$sql = "INSERT INTO tag5156 (service_date,tech,history) VALUES ('$service_date','$tech','$history')"; 

$sql = 'INSERT INTO tag5156 (service_date,tech,history) VALUES ("'.$service_date.'","'.$tech.'","'.$history.'")'; 

有關爲何,看到this answer另一個問題更多信息...