I have deleted the table and started over with new data over and over and tried to figure out why my loop is adding an extra empty result at the beginning. The code that I am using for the loop is below.
$sql = "SELECT * FROM `addresses` WHERE `company_name` = '$pro_company'";
$query = $mysqli->query("$sql");
while($array[] = $query->fetch_object());
array_pop($array);
foreach($array as $listing) :
echo $listing->Taddress . " ";
echo $listing->Tcity. " ";
echo $listing->Tstate . " ";
echo $listing->Tzip . " ";
echo " <a href='edit.php?pid=". $listing->PID . "'>edit</a> |";
echo " <a href='delete.php?pid=". $listing->PID . "'>delete</a>";
echo "</a><br />";
endforeach;
The results I am getting from this loop are below.
edit | delete
14220 Parrott Ext. TestCity AL 84106 edit | delete
I am trying to learn mysqli statements so I am sure there is something I am missing.
Thanks for your help in advance.
Throw a
print_r($array);die;
Before the foreach and look at the output. This should give you an idea of where exactly the problem is located.
Most likely your database has a row with no values. Check that first. happens more often than you realise (to me anyway)
As a possible solution, you can add an extra condition before echo'ing to ensure a value exists
foreach($array as $listing) :
if($listing->Taddress){
echo $listing->Taddress . " ";
Personally, I'd find the cause first. print_r will help
Related
I would like to do something quite simple but I dont know the right code in php.
I have a variable $go which content is GO:xxxxx
I would like to query that if the content of a variable has the pattern "GO:" and something else, echo something, but if not, echo another thing
I want to declare an if statement like:
if (preg_match('/GO/', $go) {
echo "something";
}
else {
echo "another thing";
But I cannot make it work...
I want to embed this statement between this portion of my script:
$result = mysqli_query($enlace,"select count(distinct name2) as total from " . $table . " where go_id like '%" . $go . "%'");
$items = mysqli_fetch_assoc($result);
echo "<br/>";
echo "<b> The total number of genes according to GO code is:</b> " . $items['total'];
mysqli_free_result($result);
$result2 = mysqli_query($enlace,"select count(distinct name2) as total from " . $table . " where db_object_name like '%" . $go . "%'");
$items2 = mysqli_fetch_assoc($result2);
echo "<br/>";
echo "<b>The total number of genes according to GO association is:</b> " . $items2['total'];
mysqli_free_result($result2);
As it is right now, the variable $go can have a value like GO:xxxx or a random sentence, and, with this code, I get two strings, one with value 0 and another with value according to the total apperances matching $go content.
What I want is to declare an if statement so that it just prints one string, the one that has the number of matches according to $go content, but not both.
Any help?
Use strpos():
if (strpos($go, 'GO:') !== false) {
echo 'true';
}
Try this
echo (!strpos($go, 'GO:')) ? "another thing" : "something";
Will surely work
I have a problem. I want a Table on my page, which shows things from a MySQL DB. This works just fine:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=********', '********', '********');
$sql = "SELECT subject, givenOn, toDate, teacher, what FROM homework";
echo "<table>";
foreach ($pdo->query($sql) as $row){
// echo "Fach:" . $row['subject'] . " - Lehrer:" . $row['teacher'] . " - Was: " . $row['what'] . $
echo "<tr>";
echo "<td>", $row['subject'], "</td>";
echo "<td>", $row['teacher'], "</td>";
echo "<td>", $row['what'], "</td>";
echo "</tr>";
}
echo "</table>";
?>
But I want another column behind the "what" - one which contains a Link to remove the Line. I dont know how its possible to say the Links what he has to delete. My idea was a PhP Page which has arguments like:
delete.php?subject=math&teacher=smith&what=p13
But this is very unsecure, isnt it?
Have u any Ideas how I can solve this problem?
dunklesToast
yes, it is.
1, you need to control who can delete an item, which is a rbac system.
2, delete should use POST rather than GET.
3,use PDOStatement::bindParam to prevent sql injection.
I couldn't understand what is wrong here. I cannot take the first row on database. I also cannot the data which are single according to $ara parameter. Here are the codes:
$ara =$_POST['ara'];
$query=$db->prepare("SELECT * FROM uyeler WHERE
name like '%".$ara."%' or
surname like '%".$ara."%' or
email like '%".$ara."%'
");
$query->execute(array());
if ($num_row = $query->fetchAll(PDO::FETCH_BOTH)){
echo "<li><a href='profil.php?id=".$num_row['user_id']."'>" .$num_row['name']. " " .$num_row['surname']."</a></li>";
}else{
echo "Hiç birşey bulunamadı!";
}
You need to correct your array call:
from
$num_row['user_id']
To
$num_row[0]['user_id']
Your code is tested and works, nice job.
what you need to correct is the following line:
echo "<li><a href='profil.php?id=" . $num_row[0]['user_id'] . "'>" . $num_row[0]['name'] . " " . $num_row[0]['surname'] . "</a></li>";
If you face such problem in the future, it is a good practice to var_dump the results like:
var_dump($num_row);
This will give you a total raw picture of the content.
I have this table:(megaoverzicht.php) (I left out the part where it connects to the db)
echo "<table border='1'><tr><th>Formulier Id</th><th>Domeinnaam</th><th>Bedrijfsnaam</th><th>Datum</th><th>Periode</th><th>Subtotaal</th><th>Dealernaam</th><th>Offerte Maken</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['formuliernummer'] . "</td>";
echo "<td>" . $row['domeinnaam'] . "</td>";
echo "<td>" . $row['bedrijfsnaam'] . "</td>";
echo "<td>" . $row['datum'] . "</td>";
echo "<td>" . $row['periode'] . "</td>";
echo "<td> € " . $row['subtotaal'] . "</td>";
echo "<td>" . $row['dealercontactpersoon'] . "</td>";
echo "<td><a href='offertemaken.php?id=" . $row->id . "'>Offerte Maken </a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I want to open offertemaken.php when the user clicks on Offerte Maken. It needs to open the form with the data from that row(id).
This is the code from (offertemaken.php)(I left out the part where it connects to the db)
<?php
$id=$_POST['id'];
$data = 'SELECT * FROM cypg8_overzicht WHERE id="$id"';
$query = mysqli_query($con,$data) or die("Couldn't execute query. ". mysqli_error());
$data2 = mysqli_fetch_array($query);
?>
<form>
<div class="formcontainer" onmousemove="">
<input type="text" name="datum" id="datum" value="<?php echo $data2[datum]?>">
<input type="text" name="formuliernummer" id="formuliernummer" value="<?php echo $data2[formuliernummer]?>">
<input type="text" name="periode" id="periode" value="<?php echo $data2[periode]?>">
<input type="text" name="domeinnaam" id="domeinnaam" value="<?php echo $data2[domeinnaam]?>">
<input type="text" name="bedrijfsnaam" id="bedrijfsnaam" value="<?php echo $data2[bedrijfsnaam]?>">
<input type="text" name="dealercontactpersoon" id="dealercontactpersoon" value="<?php echo $data2[dealercontactpersoon]?>">
</div><!--/.formcontainer-->
</form>
I cant get it to work. I am missing something I think! I make an error in the codes below:
echo "<td><a href='offertemaken.php?id=" . $row->id . "'>Offerte Maken </a></td>";
$id=$_POST['id'];
$data = 'SELECT * FROM cypg8_overzicht WHERE id="$id"';
I have been looking at a lot of tutorials but cant understand what i am doing wrong. Here a list to show that i am not just asking but actually have been looking for a solution by myself.
http://www.daniweb.com/web-development/php/threads/341921/-php-mysqli-update-database-using-id-syntax-help-requested-
http://www.codeofaninja.com/2012/01/phpmysqli-update-record.html
I have looked at many more but i don’t want to bother all of you with an extreme long list of links. And i am not allowed because my rep is not big enough! Dont downvote me please!
Question
I want to open offertemaken.php when the user clicks on Offerte Maken. It needs to open the form with the data from that row(id)?
Edit 1 Getting closer to the endresult
I found out(thanks to Cuba32) that the link in megaoverzicht.php was doing nothing so i changed the following
<a href='offertemaken.php?id=" . $row->id . "'>
to
<a href='offertemaken.php?id=" . $row['id'] . "'>
Now it is creating these kind of links:
something/formulieren/overzichten/offertemaken.php?id=24
This is a good thing(i think) but the form that opens is blank so offertemaken.php is doing nothing with the id???
Edit 2 (Thanks to Cube32)
Since yesterday the code has changed quite a bit. I belive that megaoverzicht.php is finished it sends the link as described in edit 1. The only problem is know in offertemaken.php. Below i will put in the code.
$con = mysqli_connect($server,$username,$password,$database);
if (!$con){
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,$database);
$id=$_GET['id'];
if($data = mysqli_prepare($con, 'SELECT * FROM cypg8_overzicht WHERE id="?"'))
{
/* bind parameters for markers */
mysqli_stmt_bind_param($data, "s", $id);
/* execute query */
mysqli_stmt_execute($data);
$data2 = mysqli_stmt_fetch($data);
But this code gives me the following error.
Warning: mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement in line 31. Line 31:
mysqli_stmt_bind_param($data, "s", $id);
I dont know how to solve this part. I will offcourse be looking on the internet to try and find a solution but if anyone knows it please post it. Thanks in advance.
Edit 3<= No more error (Thanks to Your Common Sense)
by changing WHERE id="?"' into WHERE id=?' i no longer have the error. But still it is not showing anything in the input fields
Edit 4<= Getting to confused and going back to original code.
Thanks for everyone who got me so far. But I can't see the forest anymore through the trees. I am going back to the original code and try to solve that. So the code is now as follows:
$id=$_GET['id'];
$data = 'SELECT * FROM cypg8_overzicht WHERE id="$id"';
$query = mysqli_query($con,$data) or die("Couldn't execute query. ". mysqli_error());
$data2 = mysqli_fetch_array($query);
error_reporting(E_ALL);
But this gives the following errors inside the input fields:
Notice: Use of undefined constant formuliernummer - assumed 'formuliernummer' in offertemaken.php on line 37
This error goes for all the input fields.
Edit 5
Fixed this by changing <?php echo $data2[formuliernummer]?> to <?php echo $data2['formuliernummer']?> but it is still not showing the information.
Edit 6 THE SOLUTION
I added the answer to the question below. Just look for answer written by HennySmafter.
Thanks to:
Cube32, SITDGNymall, Your Common Sense. Thanks all of you for helping me find the solution.
It took me a while but i found the answer.
megaoverzicht.php
echo "<td><a href='offertemaken.php?id=" . $row['id'] . "'>Offerte Maken </a></td>";
offertemaken.php
// Check whether the value for id is transmitted
if (isset($_GET['id'])) {
// Put the value in a separate variable
$id = $_GET['id'];
// Query the database for the details of the chosen id
$result = mysqli_query($con,"SELECT * FROM cypg8_overzicht WHERE id = $id");
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = "Invalid query: " . mysqli_error($result) . "\n";
$message .= "Whole query: " . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(),etc.
while ($row = mysqli_fetch_assoc($result)) {
echo $row['formuliernummer'] . "\n";
echo $row['domeinnaam'] . "\n";
echo $row['bedrijfsnaam'] . "\n";
echo $row['datum'] . "\n";
echo $row['periode'] . "\n";
}
} else {
die("No valid id specified!");
}
It is not showing the values in the input boxes because there are no input boxes into the echo but those can be easily added I imagine.
In reference to the edit 1:
You are referencing the variables by association, but are outputing the mysql as a default array. instead of
$data2 = mysqli_fetch_array($query);
Try this:
$data2 = mysqli_fetch_assoc($query);
Or:
$data2 = mysqli_fetch_array($query, MYSQLI_ASSOC);
Also, do you have error reporting turned on? If so, then if the array contains no data you should be getting warnings of some kind. If not, a good test is:
error_reporting(E_ALL);
This will warn you about any places where a variable is unset or a array is empty. Another good test is to simply echo out your query, which will tell you if there's any errors in the query itself(which can save some time). If you're not going to go the Prepared Statements route(which is highly encouraged), you can simply echo out $data into your script.
Hi I have the details of two car drivers getting pulled from a database but then I want to just grab one of the two and echo it out in php. I am able to take both drivers details and echo them both out using a while loop but not target just one of them:
By using the code below I echo out both records. Is there a way to just grab one of them?
while($row = mysql_fetch_array($result))
{
echo "A driver:<br />";
echo "Name:". $row['FirstName'] . " " . $row['LastName']."<br />";
echo "Latitude:". $row['CoordLat']."<br />";
echo "Longitude:". $row['CoordLong'];
$driverlat = $row['CoordLat'];
$driverlong = $row['CoordLong'];
}//end of while loop
Just remove the while loop ?
$row = mysql_fetch_array($result))
echo "A driver:<br />";
echo "Name:". $row['FirstName'] . " " . $row['LastName']."<br />";
echo "Latitude:". $row['CoordLat']."<br />";
echo "Longitude:". $row['CoordLong'];
$driverlat = $row['CoordLat'];
$driverlong = $row['CoordLong'];
You don't need a while loop at all.
Just do $row = mysql_fetch_array($result); on it's own separate line.
This will display the first row result.
You can just put a break; at the end of the while loop. Then only the first row is processed.
Better way would be to use no loop: Just assign the $row and do the render stuff.