I have a webpage which sends a query to my MySQL database to retrieve information about my company's suppliers and echos the results (all fine so far). It then makes a second query to check if there are any known contacts associated with that supplier:
while ($row = mysqli_fetch_array($result)) {
$contact = $row["contact"];
echo $row["name"] . "<br>" . $row["category"] . "<br>" . "Telephone: " .
$row["telephone"] . "<br>" . "Fax: " . $row["fax"] . "<br>" .
$row["email"] . "<br>" . $row["address"] . "<br>" .
$row["postcode"] . "<br><br>" . "Contact: " .
$contact . "<br>";
$resultContact = mysqli_query($con,"SELECT * FROM contacts WHERE name =
'$contact'");
while($rowContact = mysqli_fetch_array($resultContact)) {
echo $rowContact["telephone"] . "<br>" . $rowContact["email"];
echo "<br>";
}
}
($row is from the first query and works.) However, echo $rowContact["telephone"] . "<br>" . $rowContact["email"]; just prints whitespace.
Thanks
EDIT: if I use a string literal in the Where clause, it works as it should. However, I used variables in exactly the same way as I am now in the first query and that works! I am confused :(
EDIT 2: the first query: $result = mysqli_query($con,"SELECT * FROM suppliers WHERE LOWER(name) = LOWER('$searchText')");
EDIT 3: the result of doing var_dump($resultContact);: object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(4) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) } I'm guessing this means that the query is coming back with the table ok, but the Where clause isn't comparing the contact name as I want it to...
Your query must be mysql_query, not mysqli_query.
This is the correct code:
//Your db connection
mysql_connect('localhost','root','');
mysql_select_db('database');
//queries and data
$result = mysql_query("SELECT * FROM suppliers WHERE LOWER(name) = LOWER('$searchText')");
while ($row = mysql_fetch_array($result)) {
$contact = $row["contact"];
echo $row["name"] . "<br>" . $row["category"] . "<br>" . "Telephone: " .
$row["telephone"] . "<br>" . "Fax: " . $row["fax"] . "<br>" .
$row["email"] . "<br>" . $row["address"] . "<br>" .
$row["postcode"] . "<br><br>" . "Contact: " .
$contact . "<br>";
$resultContact = mysql_query("SELECT * FROM contacts WHERE name =
'$contact'");
while($rowContact = mysql_fetch_array($resultContact)) {
echo $rowContact["telephone"] . "<br>" . $rowContact["email"];
echo "<br>";
}
}
Related
I am trying to make a button on a page that prints out data from the database and then you can press 2 different buttons, one that deletes them from the database and the other one inserts it into another table in the database and deletes the data from the database, but it keeps inserting it twice into the new table and I have no clue why, this here prints out the data and session variables + buttons:
if(!isset($_POST['orderby'])) {
foreach ($requests as $row) {
echo "<div class='requests'>" . "<li class='refunds'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
} else {
foreach ($pergele as $row) {
echo "<div class='requests'>" . "<li class='refunds2'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
}
and this should insert it into the database once and delete the data from the old table:
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
$custname = $_SESSION['custname'];
$prodname = $_SESSION['prodname'];
/* Query to do whatever here */
$wpdb->insert("wp_acceptedrequests", [
"customer_name" => "$custname",
"name_product" => "$prodname",
"date" => date("Y/m/d/G:i:sa") ,
]);
$wpdb->query("DELETE FROM wp_refundrequests WHERE request_id = $accept");
}
What makes them insert twice and how do I prevent it from doing that?
I just ran into a similar situation where $wpdb inserts where being duplicated.
In my case it was happening if I was authenticated and browser inspector was open.
I'm working on a page, where users post their betting picks. In MySQL I have the table bets (id, event, pick, odds, stake, analysis, status, profit).
I would like to check if 'status' is empty in MySQL, but the if() statement is not working. If it's empty, it should output all the bets from a user. The code posted is in a for loop.
So what is wrong with the if() statement? And is there any better way to do this?
$result = queryMysql("SELECT * FROM bets WHERE user='$user'");
$row = mysqli_fetch_array($result);
if ('' !== $row['status']) {
echo "Status: " . $status . "</div>" .
"Published by: " . $user . "<br>" .
"PICK: " . $pick . "<br>" .
"Odds: " . $odds . "<br>" .
"Stake: " . $stake . "<br>" .
nl2br($analysis) ;
}
You are using identical comparison, which would check for type & value both. === or !== as this involves comparing the type as well as the value.
Instead try -
if (!empty($row['status'])) { // assuming status would hold only strings (not false/0 etc)
Or
if ($row['status'] != '') {
Use mysqli_num_rows(). If its greater then 0 then we can say that query containing result so we can further proceed.
$result = queryMysql("SELECT * FROM bets WHERE user='$user'");
if(mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_array($result);
if ($row['status'] != "") {
echo "Status: " . $status . "</div>" .
"Published by: " . $user . "<br>" .
"PICK: " . $pick . "<br>" .
"Odds: " . $odds . "<br>" .
"Stake: " . $stake . "<br>" .
nl2br($analysis) ;
}
}
For status check you can use any of below method
if ($row['status'] != "") { }
OR
if (!empty($row['status'])) { }
If it's empty, it should output all the bets from a user. The code posted is in a for loop.
Since you are checking if it's empty, your if statement should be the other way round:
if ($row['status'] == '') {
Alternatively, you can use mysqli_num_rows() get the number of rows:
Returns the number of rows in the result set.
$result = queryMysql("SELECT * FROM bets WHERE user='$user'");
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_array($result);
echo "Status: " . $status . "</div>" .
"Published by: " . $user . "<br>" .
"PICK: " . $pick . "<br>" .
"Odds: " . $odds . "<br>" .
"Stake: " . $stake . "<br>" .
nl2br($analysis) ;
}
Also, there isn't such function called queryMysql():
$result = queryMysql("SELECT * FROM bets WHERE user='$user'");
It should be mysqli_query(). For mysqli_query(), the connection parameter is needed.
$result = mysqli_query($conn, "SELECT * FROM bets WHERE user='$user'");
I have two tables that looks like this:
Put it in images since i dont know how to draw a table here.
My problem is that i can't seem to make a query or anything in my php that will allow me to load the report and the 5 images, so that i can display the images where i want to on the page. As i do it now it loads the report five times and one image in each report.
The edited code after #Terminus suggestions
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $row["beskrivelse"]."<br>";
} else {
echo "0 results";
}
Can anyone help me do this? I have been searching on google for four days now, without any success.
Do as #Kenney suggested and remove the part where you echo the report from the loop.
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
$beskrivelse = $row["beskrivelse"];
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $beskrivelse . "<br>";
} else {
echo "0 results";
}
I am having a problem displaying a JOIN statement. When I add
WHERE id = " . $team_id;
The information that is on the database will not display, but when I remove that line the information will correctly join and display on the "teaminfo.php " page, but it will display all of the data instead of the data that is unique to that id. Also when I remove the JOIN the the data that is unique to the id will display. Can anyone tell me whats wrong here. Any help will be great. Than you.
teaminfo.php
<html>
<head>
<title>Team Info page</title>
</head>
<body>
<?php
include 'connect.php';
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting
JOIN fieldscouting
ON pteam_number = fteam_number
WHERE id = " . $team_id;
if ($result = mysqli_query($mysqli, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
// Write the data of the team
echo "<br />";
echo "Pit scouting";
echo "<dt>Team:</dt><dd>" . $row["pteam_number"] . " " . $row["pteam_name"] . "</dd>";
echo "<dt>Auto:</dt><dd>" . $row["pauto"] . "</dd>";
echo "<dt>Drive:</dt><dd>" . $row["pdrive"] . "</dd>";
echo "<dt>Objetcs With No Problem?</dt><dd>" . $row["pobjNoProblem"] . "</dd>";
echo "<dt>Objects They have a problem with?</dt><dd>" . $row["pobjWithProblem"] . "</dd>";
echo "<dt>Can they shoot? If yes from where and how acc</dt><dd>" . $row["pshoot"] . "</dd>";
echo "<dt>Extra Notes about their robot?</dt><dd>" . $row["pdrive"] . "</dd>";
echo"<br />";
echo "Field Scouting ";
echo "<dt>Team Number:</dt><dd>" . $row["fteam_number"] . "</dd>";
echo "<dt>Auto:</dt><dd>" . $row["fauto"] . "</dd>";
echo "<dt>Drive:</dt><dd>" . $row["fdrive"] . "</dd>";
echo "<dt>Objetcs With No Problem?</dt><dd>" . $row["fobjNoProblem"] . "</dd>";
echo "<dt>Objects They have a problem with?</dt><dd>" . $row["fobjWithProblem"] . "</dd>";
echo "<dt>Shots taken</dt><dd>" . $row["fshots_taken"] . "</dd>";
echo "<dt>Shorts made</dt><dd>" . $row["fshots_made"] . "</dd>";
echo "<dt>Extra Notes</dt><dd>" . $row["fnotes"] . "</dd>";
}
mysqli_free_result($result);
}
// Close the database connection
mysqli_close($mysqli);
?>
<p>Return to the list</p>
</body>
</html>
Palmetto.php
<?php
include 'connect.php';
// SQL query
$query = "SELECT * FROM pitscouting ORDER BY pteam_number";
if($result = mysqli_query($mysqli, $query)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$name = $row['pteam_number'] . " " . $row['pteam_name'];
// Create a link to teaminfo.php with the id-value in the URL
$strLink = "<a href = 'teaminfo.php?id= " . $row['id'] . "'>" . $name . "</a>";
// List link
echo "<li>" . $strLink . "</li>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $query. " . mysqli_error($mysqli);
}
// Close connection
mysqli_close($mysqli);
?>
If your tables both have an ID field you will have to specify which table you want to get the data from.
WHERE pitscouting.id = " . $team_id;
or
WHERE fieldscouting.id = " . $team_id;
Please do mention the sql injection in you're code
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting
JOIN fieldscouting
ON pteam_number = fteam_number
WHERE id = " . $team_id;
please take a look at prepared statements, to prevent sql injections in youre code
Try putting an alias.
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting p
JOIN fieldscouting f
ON p.pteam_number = f.fteam_number
WHERE p1.id = " . $team_id;
The above code is designed to display info stored in sql table. everything is corresponding to the titles in the table and in the correct order. however the page it is from is only displaying the first 2 columns and not the others. everything looks as if it is in order to me. is my statement wrong?
<?php
$con=mysqli_connect("xxx","y","y","yyyy");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tripdata ");
while($row = mysqli_fetch_array($result))
{
echo $row['trip_id'] . " " . $row['image'] . " " . $row['date'] . " " . $row['destination'] . " " . $row['hl'] . " " . $row['cost'] . " " . $row['blurb'] . " " . $row['whatinc'] . " " . $row['whatopt'] . " " . $row['itin'] . " " . $row['depinfo'] . " " . $row['ppcode'];
echo "<br>";
}
mysqli_close($con);
?>
maybe you can try using assoc
while($row = mysqli_fetch_assoc($result))
{
echo $row['trip_id'] . " " . $row['image'] . " " . $row['date'] . " " . $row['destination'] . " " . $row['hl'] . " " . $row['cost'] . " " . $row['blurb'] . " " . $row['whatinc'] . " " . $row['whatopt'] . " " . $row['itin'] . " " . $row['depinfo'] . " " . $row['ppcode'];
echo "<br>";
}
i usual use this and no problem
assoc is index name based on field name
but array is based on number (0, 1, 2, 3)
Strange. Try do loop instead:
do {
echo $row['trip_id'] . " " . $row['image'] . " " . $row['date'] . " " . $row['destination'] . " " . $row['hl'] . " " . $row['cost'] . " " . $row['blurb'] . " " . $row['whatinc'] . " " . $row['whatopt'] . " " . $row['itin'] . " " . $row['depinfo'] . " " . $row['ppcode'];
echo "<br>";
} while($row = mysqli_fetch_array($result));