2015-12-21 51 views
0

我正在嘗試獲取用戶使用複選框檢查的sql行並將該ID發佈到將用戶選定行保存到db的腳本中,以便他們可以在稍後的數據中提取「已保存」的行。PHP MySQL根據用戶複選框將行ID保存到數據庫

下面是我的代碼 - 問題是當我發佈複選框值顯示爲「1」,我不知道爲什麼會發生這種情況。所有複選框值都顯示爲「1」。

require('./wp-blog-header.php'); 

$current_user = wp_get_current_user(); 

$school = $_POST['school']; 

$connection = mysql_connect('198.71.225.63:3306', 'newmslsuper', ''); 
mysql_select_db('msl_data'); 

$query = "INSERT INTO searches (ID, school, type) VALUES('$current_user->ID', '$school', '1')"; 

mysql_query($query); 

$search = mysql_query("SELECT * FROM `data` WHERE `school` LIKE '%$school%'"); 

$count=mysql_num_rows($search); 
if ($count==0) { 
    echo 'Sorry your search for'; echo " $school "; echo 'returned no results. Please try again.'; 
} 
else { 
    $fields_num1 = mysql_num_fields($search); 

    echo "<form action='save.php' method='post'>"; 
    echo "<p>Check the box next to a Scholarship you would like to save and hit the SAVE button.<p/><table><tr><th>Save Search</th>"; 

    // printing table headers 
    for($i=0; $i<$fields_num1; $i++) 
    { 
     $field1 = mysql_fetch_field($search); 
     echo "<th>{$field1->name}</th>"; 
    } 
    echo "</tr>\n"; 

    // printing table rows 

    while($row = mysql_fetch_array($search)){ 
     foreach($row as $rowarray) 
      while($row1 = mysql_fetch_row($search)){ 
       echo "<tr>"; 
       echo "<td><input type='checkbox' value='$rowarray' name='cell'></td>"; 
       // $row is array... foreach(..) puts every element 
       // of $row1 to $cell1 variable 
       foreach($row1 as $cell1) 
        echo "<td>$cell1</td>"; 
       echo "</tr>\n"; 
      } 
    } 
} 

echo "<input type='submit' value='SAVE'>"; 

mysql_close(); //Make sure to close out the database connection 
+0

如果複選框被選中,您將始終將值設爲1。您需要做的是,根據數據庫標識值命名複選框。 – Maximus2012

+0

你不應該那樣做。 http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – miken32

回答

0

您的複選框應該是數組,因爲它們是多個。之所以你把它們全部設爲1,因爲它們互相重疊。

<form method='post' id='form' action='page.php'> 

    <input type='checkbox' name='checkboxvar[]' value='Option One'>1 
    <input type='checkbox' name='checkboxvar[]' value='Option Two'>2 
    <input type='checkbox' name='checkboxvar[]' value='Option Three'>3 
    <input type='submit'> 
</form> 


    <?php 
if(isset($_POST['submit']){ 
    $v = $_POST['checkboxvar']; 

    foreach ($v as $key=>$value) { 
      echo "Checkbox: ".$value."<br />"; 
     } 
} 
?> 
+0

感謝您的幫助 - 看起來更好。這裏是現在的問題 - 當發佈數組 –

+0

回聲「​​」; –

+0

while($ row = mysql_fetch_array($ search)){ foreach($ row as $ rowarray) 我期待從列「id」 –

0

TBH,這件事情一團糟。你的問題的基礎是:a)只有一個命名元素(如另一個答案指出)和b)試圖給它一個數組作爲一個值。但即使在修復之後,這也永遠不會起作用。

你有你的數據庫結果在四個獨立的循環內,我不知道那裏有什麼想法。而且,如果您向我展示了此網頁,則只需單擊一下即可輕鬆擦除整個數據庫。

下面是5分鐘工作後的樣子。我仍然不會稱這是一個合理的腳本,但希望它會給你一些東西來學習。您需要優先了解preventing SQL injection,第一種方法是停止使用a database engine that's been unsupported 5年。因爲它現在已經構建到PHP中近十年了。它提供了便捷的方法將結果集轉儲到數組中。

<html> 
<head> 
<link rel="stylesheet" type="text/css" href="results.css"> 
</head> 
</html> 

<?php 
require('./wp-blog-header.php'); 
$current_user = wp_get_current_user(); 
$school = $_POST['school']; 
$db = new PDO("mysql:host=198.71.225.63;dbname=msl_data", "newmslsuper", ""); 
$stmt = $db->prepare("INSERT INTO searches (ID, school, type) VALUES(?,?,?)"; 
$stmt->execute(array($current_user->ID, $school, 1)); 

$stmt = $db->prepare("SELECT * FROM `data` WHERE `school` LIKE ?"); 
$stmt->execute(array("%$school%")); 
// put it in an array. presto! 
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); 
if (count($result) === 0) { 
    echo "Sorry your search for '$school' returned no results. Please try again."; 
} 
else { 
    $fields = array_keys($result[0]); 

    echo "<form action='save.php' method='post'>"; 
    echo "<p>Check the box next to a Scholarship you would like to save and hit the SAVE button.<p/><table><tr><th>Save Search</th>"; 

    // assume "id" field is first 
    unset($fields[0]); 
    // printing table headers 
    foreach($fields as $field) { 
     echo "<th>$key</th>"; 
    } 
    echo "</tr>\n"; 

    // printing table rows 
    // just one loop 
    foreach($result as $row) { 
     echo "<tr>"; 
     // assume the column is named "id" 
     echo "<td><input type='checkbox' value='$row[id]' name='cell[]'></td>"; 
     unset($row["id"]); 
     foreach($row as $cell) { 
      echo "<td>$cell</td>"; 
     } 
     echo "</tr>\n"; 
    } 

    echo "<input type='submit' value='SAVE'>"; 
    echo "</form>"; 
} 
?> 
相關問題