2016-05-03 7 views
1

PHPでかなりのnoobです。私は、mysqlテーブルからデータを取得し、それをテキストボックスに保存するPHPスクリプトを作成した後、テキストボックスを編集し、mysqlを更新するための更新ボタンをクリックすることができます。結果が非常に多いので、スクリプトにpaginatorを追加しました。今、私は検索エンジンを追加しようとしています。検索エンジンが私のページャーで動作しません

<html> 
<head> 
<title>TITLE</title> 
</head> 
<style> 
/* #container { 
display:inline-block;width:100%;float:left;clear:left 
} 
table{ 
    width:100%; 
} 
td{ 
    border: 0; 
} 
td {width:100%;float:left;clear:left} 
th {visibility:hidden;} */ 
</style> 
<body> 
<?php 
$num_rec_per_page=5; 
$con = mysql_connect("localhost","root","pass"); 
if (!$con) { 
die("connE: " . mysql_error()); 
} 
mysql_select_db("database_name",$con); 

if(isset($_POST['update'])){ 
$UpdateQuery = "UPDATE users SET 
username='$_POST[username]', 
password='$_POST[password]' 
WHERE username='$_POST[hidden]'"; // etc, etc ... i deleted all the stuff cause it's too many 

mysql_query($UpdateQuery, $con);  
}; 

if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $num_rec_per_page; 

$sql = "SELECT * FROM users LIMIT $start_from, $num_rec_per_page"; 

if (isset($_GET['search'])) { 
    $search_term = mysql_real_escape_string($_GET['search_box']); 
    $sql .= "WHERE username = '{$search_term}'"; 
}; 

$myData = mysql_query($sql, $con) or die (mysql_error()); 

echo "<form name='search_form' method='GET' action='users.php'> 
Search: <input type='text' name='search_box' value='Search' /> 
<input type='submit' name='search' value='Search'> 
</form>"; 

echo " 
<div id='container'> 
<table> 
<tr> 
<th>Username</th> 
<th>Password</th> 
</tr>"; // etc, etc ... i deleted all the stuff cause it's too many 
while($record = mysql_fetch_array($myData)){ 
echo "<form action=users.php method=post>"; 
echo "<tr>"; 
echo "<td>" . "<input type=text name=username value='" . $record['username'] . "' </td>"; 
echo "<td>" . "<input type='text' name='password' value='" . $record['password'] . "' </td>"; 
echo "<td>" . "<input type='hidden' name='hidden' value='" . $record['username'] . "' </td>"; 
echo "<td>" . "<input type='submit' name='update' value='update'" . " </td>"; 
echo "</tr>"; // etc, etc ... i deleted all the stuff cause it's too many 
echo "</form>"; 
echo "<br />"; 
} 
echo "</table>"; 

$sql = "SELECT * FROM users"; 
$myData = mysql_query($sql, $con); 
$total_records = mysql_num_rows($myData); //count number of records 
$total_pages = ceil($total_records/$num_rec_per_page); 

echo "</div>"; 

echo "<a href='users.php?page=1'>"."<img style='margin-right:10px;margin-top:-3px;' src='img/prev.png'>"."</a> "; // Goto 1st page 

for ($i=1; $i<=$total_pages; $i++) { 
      echo "<a href='users.php?page=".$i."'>&nbsp;".$i."&nbsp;</a> "; 
}; 

echo "<a href='users.php?page=$total_pages'>"."<img style='margin-left:11px;margin-top:-3px;' src='img/next.png'>"."</a> "; // Goto last page 

mysql_close($con); 
?> 

</body> 
</html> 

私はメッセージを取得する検索時に:ここに私のコードは宣言されていない変数:5WHERE は、私はすでに述べたように、私はページネータを削除すると、それは検索が機能し、すべての罰金だが、それは有効ページネータでは動作しません。

答えて

1

LIMIT句は、WHERE句の後にする必要があります。

// your code 

if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $num_rec_per_page; 

$sql = "SELECT * FROM users"; 

if (isset($_GET['search'])) { 
    $search_term = mysql_real_escape_string($_GET['search_box']); 
    $sql .= " WHERE username = '{$search_term}'"; 
} 

$sql .= " LIMIT $start_from, $num_rec_per_page"; 

$myData = mysql_query($sql, $con) or die (mysql_error()); 

// your code 

追記:だからあなたのコードは次のようにする必要がありますmysql_*機能を使用しないでください、彼らは、PHP 5.5で廃止されており、PHP 7.0で完全に削除されます。代わりにmysqliまたはpdoを使用してください。 And this is why you shouldn't use mysql_* functions

+0

これは完全に機能しました。答えてくれてありがとう、そしてSidenote。 –

関連する問題