2011-08-19 33 views
0

更新:現在解決 - 謝謝大家!無法在php腳本中執行sql INSERT查詢(mysql_query)。 PHP/MySQL - 時間敏感

修復:我有一個名爲「referenced_by」的列,在我的代碼中它被稱爲「referenced_by_id」 - 所以它試圖插入到一個不存在的列 - 一旦我解決了這個問題,它決定工作!

我在這個項目上工作的時間有限。時鐘在滴答滴答。

我想將$ php_variables插入到名爲「clients」的TABLE中。

我一直在努力幾個小時才能使這個腳本工作,我得到它的工作一次,但後來我意識到我忘了一個領域,所以我不得不添加另一列到表,當我更新它停止工作的腳本。我恢復了,但現在它仍然無法正常工作,我只是感到很沮喪。

<?php 

error_reporting(E_ALL); 
ini_set("display_errors", 1); 

if (!isset($_COOKIE["user"])) 
{ 
    header ("Location: ./login.php"); 
} 

else 
{ 
    include ("./source.php"); 
    echo $doctype; 
} 

$birthday = $birth_year . "-" . $birth_month . "-" . $birth_day; 
$join_date = date("Y-m-d"); 

$error_type = 0; 

$link = mysql_connect("SERVER", "USERNAME", "PASSWORD"); 

if (!$link) 
{ 
    $error = "Cannot connect to MySQL."; 
    $error_type = 1; 
} 

$select_db = mysql_select_db("DATABASE", $link); 

if (!$select_db) 
{ 
    $error = "Cannot connect to Database."; 
    $error_type = 2; 
} 

if ($referred_by != "") 
{ 
    $result = mysql_query(" 
    SELECT id FROM clients WHERE referral_code = $referred_by 
    "); 

    if (!$result) 
    { 
     $error = "Cannot find referral."; 
     $error_type = 3; 
    } 

    while ($row = mysql_fetch_array($result)) 
    { 
     $referred_by_id = $row['id']; 
    } 
} 

else 
{ 
    $referred_by_id = 0; 
} 

$first_name = mysql_real_escape_string($_POST['first_name']); 
$last_name = mysql_real_escape_string($_POST['last_name']); 
$birth_month = mysql_real_escape_string($_POST['birth_month']); 
$birth_day = mysql_real_escape_string($_POST['birth_day']); 
$birth_year = mysql_real_escape_string($_POST['birth_year']); 
$email = mysql_real_escape_string($_POST['email']); 
$address = mysql_real_escape_string($_POST['address']); 
$city = mysql_real_escape_string($_POST['city']); 
$state = mysql_real_escape_string($_POST['state']); 
$zip_code = mysql_real_escape_string($_POST['zip_code']); 
$phone_home = mysql_real_escape_string($_POST['phone_home']); 
$phone_cell = mysql_real_escape_string($_POST['phone_cell']); 
$referral_code = mysql_real_escape_string($_POST['referral_code']); 
$referred_by = mysql_real_escape_string($_POST['referred_by']); 
$organization = mysql_real_escape_string($_POST['organization']); 
$gov_type = mysql_real_escape_string($_POST['gov_type']); 
$gov_code = mysql_real_escape_string($_POST['gov_code']); 

$test_query = mysql_query 
(" 
INSERT INTO clients (first_name, last_name, birthday, join_date, email, address, city, state, zip_code, 
phone_home, phone_cell, referral_code, referred_by_id, organization, gov_type, gov_code) 
VALUES ('".$first_name."', '".$last_name."', '".$birthday."', '".$join_date."', '".$email."', '".$address."', '".$city."', '".$state."', '".$zip_code."', 
'".$phone_home."', '".$phone_cell."', '".$referral_code."', '".$referred_by_id."', '".$organization."', '".$gov_type."', '".$gov_code."') 
"); 

if (!$test_query) 
{ 
    die(mysql_error($link)); 
} 

if ($error_type > 0) 
{ 
    $title_name = "Error"; 
} 

if ($error_type == 0) 
{ 
    $title_name = "Success"; 
} 

?> 


<html> 
    <head> 
     <title><?php echo $title . " - " . $title_name; ?></title> 
     <?php echo $meta; ?> 
     <?php echo $style; ?> 
    </head> 
    <body> 
     <?php echo $logo; ?> 
     <?php echo $sublogo; ?> 
     <?php echo $nav; ?> 
     <div id="content"> 
      <div id="main"> 

       <span class="event_title"><?php echo $title_name; ?></span><br><br> 

       <?php 

       if ($error_type == 0) 
       { 
        echo "Client was added to the database successfully."; 
       } 

       else 
       { 
        echo $error; 
       } 

       ?> 

      </div> 
      <?php echo $copyright ?> 
     </div> 
    </body> 
</html> 
+0

請使用mysql_query($ query)或die(mysql_error())輸出錯誤消息; – Headshota

+0

mysql_error()被禁用。 –

+0

只是一個建議:使用參數化查詢,否則你將陷入更多的麻煩(SQL注入,無效的SQL等)。 –

回答

0

您在線路81上已經錯誤:

else 
{ 
    $referred_by_id = 0; 
} 

我沒有看到IF之前構建,進行適當的修正,並再次運行該腳本。

+0

修復了有問題的代碼 - 但仍然無效:/ –

+0

這是與錯誤標記的列結合使用 - 謝謝Tuga! –

+0

我很高興我可以幫助:) –

0

不看錶的結構,以確保所有的領域都在那裏,我要承擔這件事情與數據。

數據中的任何引號都會導致問題(包括SQL注入安全漏洞)。你應該包裝每個_POST [] $用mysql_escape_string()和如:

$first_name = mysql_real_escape_string($_POST['first_name']); 

編輯:進一步調試...

正如有人建議(對不起,找不到評論),嘗試:

$sql = " 
    INSERT INTO clients (first_name, last_name, birthday, join_date, email, address, city, state, zip_code, 
    phone_home, phone_cell, referral_code, referred_by_id, organization, gov_type, gov_code) 
    VALUES ('".$first_name."', '".$last_name."', '".$birthday."', '".$join_date."', '".$email."', '".$address."', '".$city."', '".$state."', '".$zip_code."', 
     '".$phone_home."', '".$phone_cell."', '".$referral_code."', '".$referred_by_id."', '".$organization."', '".$gov_type."', '".$gov_code."' 
    )"; 

// Debug: 
print "<pre>". $sql ."</pre>"; 

mysql_query($sql); 

提交表單時應該打印出SQL語句。採取該SQL語句,並嘗試直接在MySQL中執行它,看看它是否工作,或者它是否產生錯誤。

+0

我不是太在意注射,因爲它不是公共頁面。但是,無論如何,如果它有助於解決錯誤,我會嘗試。我會在一兩分鐘內回覆。 –

+0

好的,這並沒有解決任何問題:/ –

2

絕對不能按原樣工作。看起來你有一個500錯誤,因爲你還有一個缺少的if:

else 
{ 
    $referred_by_id = 0; 
} 

否則,你需要發佈你的數據庫模式。

另外,請注意,你真的需要很長的路程與這段代碼,這使得難以閱讀&保持。你也缺少對SQL注入的任何檢查......你真的需要通過mysql_real_escape_string來傳遞(事實上,你應該使用mysqli,因爲mysql接口在幾年前基本上被棄用了)。

$keys = array('first_name', 
    'last_name', 
    'birthday', 
    'join_date', 
    'email', 
    'address', 
    'city', 
    'state', 
    'zip_code', 
    'phone_home', 
    'phone_cell', 
    'referral_code', 
    'referred_by_id', 
    'organization', 
    'gov_type', 
    'gov_code'); 

$_REQUEST['birthdate'] = $_REQUEST['birth_year'].'-'.$_REQUEST['birth_month'].'-'.$_REQUEST['birth_day']; 
$_REQUEST['join_date'] = date('Y-m-d',time()); 

$params = array(); 
foreach ($keys as $key) 
{ 
    $params[] = mysql_real_escape_string($request[$key]); 
} 

$sql = 'INSERT INTO clients ('.implode(',', $keys).') '; 
$sql .= ' VALUES (\''.implode('\',\'', $params).'\') '; 
+0

$ params應該設置爲'$ params [] =''''。我相信mysql_real_escape_string($ _REQUEST [$ key])。 –

+0

@Doug Kress - 謝謝你,這就是我寫代碼的理由。 :) –