I need to do a mysql query that will search (in columns) nome and cognome (in the Prenotazioni table).
The problem is: the query must search name and surname or surname and name (they can also be 3 or 4 values) and I have only 1 search form. If I have two john with different surnames (black and white), and i put in search form john black or white john, The query will give me both results
How i can fix it?
index.php
<table border="10">
<tr>
<td align="center"><b>CERCA</b></td>
</tr>
<tr>
<td>
<table>
<form method="post" action="./cerca.php">
<tr>
<td>Cerca:</td>
<td width="250" align="left"><input type="text" autocomplete="off" size="70" name="ricerca"/>
</td>
</tr>
<tr>
<td></td>
<td align="left"><input style="width:100px;" type="submit" value="Cerca"/>
</tr>
</form>
</table>
</td>
</tr>
</table>
cerca.php
<!doctype html><html>
<head>
<meta charset="utf-8">
<title>Ricerca</title>
</head>
<body>
<?php
include('./config.php');
$name = isset($_POST['ricerca']) ? preg_replace('/\s+/', ' ', trim($_POST['ricerca'])) : '';
if ($name != '') {
$q = 'SELECT * FROM Prenotazioni WHERE 1=2';
$parts = explode(' ', $name);
foreach ($parts as $part) {
$q .= ' OR nome LIKE \'%' . $part . '\' OR cognome LIKE \'%' . $part . '\'';
}
$exec = mysql_query($q);
if (mysql_num_rows($exec)) {
echo '<tr>
<td width="242">
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr></tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Nome</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Cognome</strong></td>
</tr>';
while($row = mysql_fetch_array($exec)) {
echo '<tr>
<td align="center" bgcolor="#FFFFFF">' . $row['nome'] . '</td>
<td align="center" bgcolor="#FFFFFF">' . $row['cognome'] . '</td> <p><br>
<br>';
}
}
else {
echo 'Errore: nessuna corrispondenza trovata.';
}
}
else {
echo 'Errore: Inserisci un nome.';
}
?>
I faced this same challenge 3days ago and this resolved it: modify line 6-11 of cerca.php to this
$parts = explode(" ",$name);
$count = count($parts);
$q = "SELECT * FROM Prenotazioni WHERE";
for($i = 0;$i < $count;$i++)
{
if($i != $count-1)
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%') AND ";
else
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%')";
}
$exec = mysql_query($q);
Given "john black","black john" and "bla joh" return the SAME RESULTS and "white john" isn't the same as "black john". I hope it helps you
make mysql query string using above mu code and run this query and make row according to you
$q = "SELECT * FROM Prenotazioni WHERE";
for($i = 0;$i < $count;$i++)
{
if($i) {
$q .= ' and ';
}
$q = $q . " (concat(nome, ' ', cognome) LIKE '%'.$parts[$i].'%')";
}
EDIT
GOOD CODE:
$name = isset($_POST['ricerca']) ? preg_replace('/\s+/', ' ', trim($_POST['ricerca'])) : '';
if ($name != '') {
$parts = explode(" ",$name);
$count = count($parts);
$q = "SELECT * FROM Prenotazioni WHERE";
for($i = 0;$i < $count;$i++)
{
if($i != $count-1)
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%') AND ";
else
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%')";
}
$exec = mysql_query($q);
if (mysql_num_rows($exec)) {
echo '<tr>
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr></tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Nome</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Cognome</strong></td>
</tr>';
while($row = mysql_fetch_array($exec)) {
echo '<tr>
<td align="center" bgcolor="#FFFFFF">' . $row['nome'] . '</td>
<td align="center" bgcolor="#FFFFFF">' . $row['cognome'] . '</td>';
}
}
else {
echo 'Errore: nessuna corrispondenza trovata.';
}
}
else {
echo 'Errore: Inserisci un nome.';
}
Related
I am trying to print out on my sites home page a horizontal table which includes a picture of a car and then below the make, model & price of the car.
Here's roughly what I have done:
<?php
$List = "";
$sql = mysql_query("SELECT * FROM Car ORDER BY listed DESC LIMIT 5");
$Count = mysql_num_rows($sql);
if ($Count > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$make = $row["make"];
$model = $row["model"];
$price = $row["price"];
$List .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="20%" valign="top"><a href="/motors/cars.php?id=' . $id . '"><img style="border:#666 1px solid;"
src="/../../motors-' . $id . '.jpg" alt="' . $status . ' ' . $title . '" width="100" height="80" border="1" /></a></td>
</tr>
<tr>
<td width="80%" valign="top" align="left"><font face="Arial" color="#000080"><B>
' . $make . ' ' . $model . '</B></font><br />' . $price . '
view car details</align></td>
</tr>
</table>';
}
} else {
$List = "No cars at present";
}
mysql_close();
?>
Can anyone help me sort this code to print out horizontally? Many Thanks!
The src of the image is most likely wrong, /../../motors... is still /motors...
you can move the images into your webapps /motors/images folder and set src as /motors/images/motors...
The table you create must be nested in another table.
<table>
<tr>
<td>
<table of a car>
</td>
<td>
<table of a car>
</td>
<td>
<table of a car>
</td>
.... etc.
</tr>
</table>
It will make sense to emit a tr every few cars to wrap to a new line.
table of a car is the html you collect in $List.
I am trying to pull data from mysql table using dynamic checkboxes created previously, however it looks like my search only displays 2 records max and if I select more than 3 checkboxes it will not return anything, so I was wondering if someone could help me figure out how to do it.
Below is my code, I really appreciate your help:
$warehouse = #$_POST['wh'];
switch($button){
case 'Submit':
if(#$_POST['wh']){
$warehouse = #$_POST['wh'];
$length = count($warehouse);
for ($i = 0; $i < $length; $i++){
//echo '<br>'.$warehouse[$i];
}
$consult = "SELECT * FROM contact_info WHERE ";
for ($i = 0; $i < $length; $i++){
$consult = $consult . "warehouse='$warehouse[$i]'";
if(!$i+1 == $length){
$consult = $consult . " OR ";
}
}
echo '<br>'.$length.'<br>';
print_r ($consult);
$response = mysqli_query($connection, $consult);
if($response){
$registry = mysqli_affected_rows($connection);
if($registry > 1){
echo '<br><table width="800" align="center" border="2" cellspacing="1" cellpadding="1">
<form action="index.php" method="post" >
<tr>
<td align="center"><strong>Warehouse</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Phone</strong></td>
<td align="center"><strong>I-net</strong></td>
<td align="center"><strong>E-mail</strong></td>
</tr>';
while($registry = mysqli_fetch_array($response, MYSQLI_ASSOC)){
echo '<form action="index.php" method=post >
<tr>
<td div align="center">'.$registry['warehouse'].'
<td div align="center">'.$registry['name'].'
<td div align="center">'.$registry['lastname'].'
<td div align="center">'.$registry['phone'].'
<td div align="center">'.$registry['inet'].'
<td div align="center">'.$registry['email'].'
</tr>';
}
echo '</form>';
echo '
</form>
</table>';
I have set up an 'orders' page that should be pretty straight-forward, requiring a for each loop to generate past orders from the DB. Within each loop, there needs to be an explode to separate each item(,), then another explode to pull the details out of each item(-). I was wondering if anyone could tell me why I'm getting the error message "Warning: mysql_num_rows() expects parameter 1" at the end of each iteration of my foreach loop? It's particularly confusing for me because the code still works, which I don't think it should if the error was true.
<?php
$sql = "SELECT * FROM orders WHERE store='$user'";
$res = mysql_query($sql);
$arrOrders = array();
while ($row = mysql_fetch_array($res)) {
array_push($arrOrders, $row);
}
?>
<table width="85%" align="center" border="0" cellpadding="5">
<?php if (count($arrOrders) > 0) { ?>
<?php foreach ($arrOrders as $key => $value) { ?>
<tr>
<td valign="top" style="font-weight: bold">
ID #<?=$value['id']?>
</td>
<td valign="top" style="font-weight: bold">
Status: <?=$value['status']?>
</td>
<td valign="top" style="font-weight: bold">
Order #<?=$value['order_ref']?>
</td>
<td align="left" valign="top" style="font-weight: bold">
<?php
$tmpProds = explode(',', $value['products']);
foreach ($tmpProds as $key2 => $value2) {
$tmpProdInfo = explode('-', $value2);
$sql2 = 'SELECT * FROM products_full WHERE id = ' . $tmpProdInfo[0];
$res2 = mysql_query($sql2);
if (mysql_num_rows($res2) > 0) { // THIS IS THE ROW THAT THE ERROR MESSAGE POINTS TO
echo $tmpProdInfo[1] + $tmpProdInfo[2] . ' x ' . mysql_result($res2, 0, 'alpha_code') . ' (' . trim(mysql_result($res2, 0, 'description')) . ')<br /><br />';
}
}
?>
</td>
</tr>
<tr>
<td width="100%" colspan="5" align="center">
<hr style="width: 100%" />
</td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td width="100%" align="center">
There are currently no complete orders.
</td>
</tr>
<?php } ?>
</table>
Thanks in advance, I think I've included everything that is relative but if anything else is needed please let me know.
Joe
Try this:
$sql2 = 'SELECT * FROM products_full WHERE id = '.$tmpProdInfo[1].'';
if (count($res2) > 0)
Also, be aware when you want to use MySQL_num_rows you should include the connection. see the manual:
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
I had a previous question on here a few minutes ago about a syntax error that was sorted. I need help getting this script working or at least for someone to point me in the right direction.
This is a search script to search by multiple fields. The search() array works fine and is a series of tickboxes with the following code:
<td width="22"><input type="checkbox" name="search[olevel = 'Yes']" id="search[olevel = 'Yes']" value="1"/>
The postcode box is a text box with the following code:
<input name="postcode[]" type="text" id="postcode[]" size="12" maxlength="12" /></td>
When I tick the olevel box it returns all the records that have Yes in the olevel field. That works as I expect.
If I put in anything in the postcode box it returns no results.
Here is the php code for the search engine.
<?php
include ('c1.php');
if ($_COOKIE["auth"] == "1") {
$display_block = "<p>You are an authorized user.</p>";
} else {
header("Location: userlogin.html");
exit;
}
doDB();
$display_block = "<h1>Results</h1>";
if (isset($_POST['search']) && !empty($_POST['search'])) {
foreach ($_POST['search'] as $key => $value) {
if ($value == 1)
$search[] = "$key";
$searchstring = implode(' AND ', $search);
$post_map = array(
'postcode' => 'candididate_contact_details.postcode'
);
}
if (isset($_POST['postcode']) && !empty($_POST['postcode'])) {
foreach ($_POST['postcode'] as $key => $value) {
if (array_key_exists($key, $post_map))
$search[] = $post_map[$key] . '=' . mysql_real_escape_string($value);
echo $searchstring;
echo $search;
$query = "SELECT candidate_id.master_id, candidate_contact_details.first_name, candidate_contact_details.last_name, candidate_contact_details.home_phone, candidate_contact_details.work_phone, candidate_contact_details.mobile_phone, candidate_contact_details.email FROM candidate_id, candidate_contact_details, qualifications, security_experience, previous_career WHERE qualifications.active = 'finished' and candidate_id.master_id = candidate_contact_details.master_id and candidate_id.master_id = qualifications.master_id and candidate_id.master_id = security_experience.master_id and candidate_id.master_id = previous_career.master_id and $searchstring";
$query_res = mysqli_query($mysqli, $query)
or die(mysqli_error($mysqli));
// $search = mysqli_query($mysqli, $query)or die(mysqli_error($mysqli));
{
$display_block .= "
<table width=\"98%\" cellspacing=\"2\" border=\"1\">
<tr>
<th>Registration Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Home Number</th>
<th>Work Number</th>
<th>Mobile Number</th>
<th>E-Mail</th>
</tr>";
while ($result = mysqli_fetch_array($query_res)) {
$regnum = $result['master_id'];
$first_name = $result['first_name'];
$last_name = $result['last_name'];
$home_phone = $result['home_phone'];
$work_phone = $result['work_phone'];
$mobile_phone = $result['mobile_phone'];
$email = $result['email'];
$display_block .= "
<tr>
<td align=\"center\">$regnum <br></td>
<td align=\"center\">$first_name <br></td>
<td align=\"center\">$last_name <br></td>
<td align=\"center\">$home_phone <br></td>
<td align=\"center\">$work_phone <br></td>
<td align=\"center\">$mobile_phone <br></td>
<td align=\"center\">$email <br></td>
</tr>";
}
$display_block .= "</table>";
}
}
}
}
?>
<html>
<head>
<title> Display results</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>
I know I am doing something wrong but cannot quite figure it out. Thanks in advance.
if I understand correctly, you have several postcodes per form.
then id="postcode[]" is wrong as there will be several id named the same in dom.
just delete that from your code.
I am using an open source calendar to display events, I am having a trouble to add extra fields to event display form. Could anyone please indicate where I need to add code so more fields can be displayed.
Please could anyone kindly help me.
Many Thanks.
Here is the code for displaying the events:
<?php
require("config.php");
require("./lang/lang." . LANGUAGE_CODE . ".php");
require("functions.php");
$auth = auth();
$id = $_GET['id'];
mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());
$sql = "SELECT d, m, y FROM " . DB_TABLE_PREFIX . "mssgs WHERE id=" . $id;
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$d = $row["d"];
$m = $row["m"];
$y = $row["y"];
$dateline = $lang['months'][$m-1] . " $d, $y";
$wday = date("w", mktime(0,0,0,$m,$d,$y));
writeHeader($m, $y, $dateline, $wday, $auth);
// display selected posting first
writePosting($id, $auth);
// give some space
echo '<img src="images/clear.gif" width="1" height="25" border="0"><br clear="all">';
// query for rest of this day's postings
$sql = "SELECT id, start_time FROM " . DB_TABLE_PREFIX . "mssgs ";
$sql .= "WHERE y = " . $y . " AND m = " . $m . " AND d = " . $d . " AND id != $id ";
$sql .= "ORDER BY start_time ASC";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result)) {
echo '<span class="display_header">' . $lang['otheritems'] . '</span>';
echo '<br clear="all"><img src="/images/clear.gif" width="1" height="3" border="0"><br clear="all">';
// display rest of this day's postings
while ($row = mysql_fetch_array($result)) {
writePosting($row[0], $auth);
echo '<img src="images/clear.gif" width="1" height="12" border="0"><br clear="all">';
}
}
echo "</body></html>";
function writeHeader($m, $y, $dateline, $wday, $auth)
{
global $lang;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>phpEventCalendar: Event Display</title>
<link rel="stylesheet" type="text/css" href="css/popwin.css">
<?php if ($auth) { ?>
<script language="JavaScript">
function deleteConfirm(eid) {
var msg = "<?php echo $lang['deleteconfirm'] ?>";
if (confirm(msg)) {
opener.location = "eventsubmit.php?flag=delete&id=" + eid + "&month=<?php echo $m ?>&year=<?php echo $y ?>";
window.setTimeout('window.close()', 1000);
} else {
return;
}
}
</script>
<?php } ?>
</head>
<body>
<!-- selected date -->
<table cellspadding="0" cellspacing="0" border="0" width="300" bgcolor="#CCFFCC">
<tr>
<td bgcolor="#CCFFCC"><span bgcolor="#CCFFCC" class="display_header"><?php echo $dateline ?></span></td>
<td bgcolor="#CCFFCC" align="right"><span class="display_header"><?php echo $lang['days'][$wday] ?></span></td>
</tr>
</table>
<img src="images/clear.gif" width="1" height="3" border="0"><br clear="all">
<?php
}
function writePosting($id, $auth)
{
global $lang;
mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());
$sql = "SELECT y, m, d, title, text, start_time, end_time, ";
$sql .= DB_TABLE_PREFIX . "users.uid, fname, lname, ";
if (TIME_DISPLAY_FORMAT == "12hr") {
$sql .= "TIME_FORMAT(start_time, '%l:%i%p') AS stime, ";
$sql .= "TIME_FORMAT(end_time, '%l:%i%p') AS etime ";
} elseif (TIME_DISPLAY_FORMAT == "24hr") {
$sql .= "TIME_FORMAT(start_time, '%H:%i') AS stime, ";
$sql .= "TIME_FORMAT(end_time, '%H:%i') AS etime ";
} else {
echo "Bad time display format, check your configuration file.";
}
$sql .= "FROM " . DB_TABLE_PREFIX . "mssgs ";
$sql .= "LEFT JOIN " . DB_TABLE_PREFIX . "users ";
$sql .= "ON (" . DB_TABLE_PREFIX . "mssgs.uid = " . DB_TABLE_PREFIX . "users.uid) ";
$sql .= "WHERE id = " . $id;
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$title = stripslashes($row["title"]);
$body = stripslashes(str_replace("\n", "<br />", $row["text"]));
$postedby = $lang['postedby'] . ": " . $row['fname'] . " " . $row['lname'];
if (!($row["start_time"] == "55:55:55" && $row["end_time"] == "55:55:55")) {
if ($row["start_time"] == "55:55:55")
$starttime = "- -";
else
$starttime = $row["stime"];
if ($row["end_time"] == "55:55:55")
$endtime = "- -";
else
$endtime = $row["etime"];
$timestr = "$starttime - $endtime";
} else {
$timestr = "";
}
if ($auth == 2 || ($auth != 0 && $_SESSION['authdata']['uid'] == $row['uid'])) {
$editstr = "<span class=\"display_edit\">";
$editstr .= "[edit] ";
$editstr .= "[delete] </span>";
} else {
$editstr = "";
}
?>
<table cellspacing="0" cellpadding="0" border="0" width="300" bgcolor="#CCFFCC">
<tr><td bgcolor="#000000">
<table cellspacing="1" cellpadding="1" border="0" width="100%">
<tr>
<td class="display_title_bg" bgcolor="#CCFFCC"><table bgcolor="#CCFFCC" cellspacing="0" cellpadding="0" border="0" width="100%"><tr>
<td bgcolor="#CCFFCC" width="100%"><span class="display_title"> <?php echo $title ?></span></td>
<td bgcolor="#CCFFCC"><img src="images/clear.gif" width="20" height="1" border="0"></td>
<td bgcolor="#CCFFCC" align="right" nowrap="yes"><span class="display_title"><?php echo $timestr ?> </span></td>
</tr></table></td>
</tr>
<tr bgcolor="#CCFFCC"><td class="display_txt_bg" bgcolor="#CCFFCC">
<table cellspacing="1" cellpadding="1" border="0" width="100%" bgcolor="#CCFFCC">
<tr bgcolor="#CCFFCC">
<td bgcolor="#CCFFCC"><span class="display_txt"><?php echo $body ?></span></td>
</tr>
<tr bgcolor="#CCFFCC">
<td bgcolor="#CCFFCC"align="right"><span class="display_user"><?php echo $postedby ?></td>
</tr>
<tr bgcolor="#CCFFCC">
<td align="right" bgcolor="#CCFFCC"><?php echo $editstr ?></td>
</tr>
</table>
</td></tr>
</table>
</td></tr></table>
<?php
}
?>
To the best of my understanding writePosting function does most of the job and you need to enhance it to display more info about current event.
I've added a new table row at the end of this code sample to illustrate the idea.
<tr bgcolor="#CCFFCC">
<td bgcolor="#CCFFCC"><span class="display_txt"><?php echo $body ?></span></td>
</tr>
<tr bgcolor="#CCFFCC">
<td bgcolor="#CCFFCC"align="right"><span class="display_user"><?php echo $postedby ?></td>
</tr>
<tr bgcolor="#CCFFCC">
<td align="right" bgcolor="#CCFFCC"><?php echo $editstr ?></td>
</tr>
<tr bgcolor="#CCFFCC">
<td align="right" bgcolor="#CCFFCC"><?php echo "some extra info" ?></td>
</tr>