I've been having trouble with this one piece of code, and can't seem to sort out the problem.
I've looked online and the error im currently getting has something to do with a failed query but as i follow the steps to solve this problem it doesent seem to work.
What I need to do is have two separate columns one for the id, and the other for the data which the ids hold and I need it to pull information from my table called tbl typeofbusiness
This is my code :
<tr>
<td>Type of Business:</td>
<td>
<select name="typeofbusiness">
<option value=''> - select type of business -</option>
<?php
$sql = "SELECT tbl_typesofbusiness.ID, tbl_typesofbusiness.Agent
FROM tbl_typesofbusiness";
$res = mysqli_query($con,$sql)or ("Error: ".mysqli_error($con));
while (list($id, $tob) = mysqli_fetch_row($res)); {
echo "<option value='$id'>$tob</option>\n";
}
?>
</select>
</td>
<td>
<span class="error">*
<?php if (isset($errors['typeofbusiness']))
echo $errors['typeofbusiness']; ?>
</span>
</td>
</tr>
This creates the dropdown box and has a default value : select type of business, but it does not seem to pull the data from the database, as there are no other options under that. It also shows an error message.
Which I no how to solve by changing or to or die but when i make that change all code under or die dosent appear and i have another 6-7 fields which really isn't convenient and even worse when I do this no error message appears.
I need someone to help me with removing these problems so that it pulls information from the database and all the other errors no longer appear.
The error is:
Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\AddLeads\addeadstemplate.php on line 256 Call Stack # Time Memory Function Location 1 0.2188 192992 {main}( ) ..\addeadstemplate.php:0 2 2.1875 205216 mysqli_fetch_row ( ) ..\addeadstemplate.php:256
While Loop close before the $error variable.
And Fetch array function not used for $error
<?php
$errors= mysql_fetch_array($res);
?>
<td><span class="error">* <?php if (isset($errors['typeofbusiness'])) { echo$errors['typeofbusiness']; }?></span></td>
</tr>
Try this.
Related
First of all I would like to let you know that I tried every other solution that I found here regarding to my topic.. But nothing worked for me!
As the title says I want to create a drop down list with data from a database!
The connection with the database is right so I don't show you the code! I have tried many ways, one is below.
The only thing that I am getting is a blank list.Thanks!
<select name="Anaktisi">
<?php
$query2="select name from books WHERE sub_ID=1";
$result2 = mysqli_query($con,$query2);
while ($row2 = mysqli_fetch_array($result2)) { ?>
<option value="<?php echo $row2['name'];?>"> </option>
<?php } ?>
</select>
I'm having some trouble with my php / mysqli code, hopefully you can help me.
I'm currently working on an online shop for a school project. Customers are able to buy things, they get an order number and I'm writing their user_id, the order number, the different products and some other things in a relation.
now the administrator should be able to see all orders.
right now it looks like this (I copied my table into a word table, so it's easier to see the structure):
part of the table
So the problem is that I have two different order numbers (80425 and 14808) and I want to show each number (and the name and adress of the custumer, too) only one time, but for each order number all different ordered products.
I imagine it like this:
part of the table (more organised)
(it's german, I hope you still get what I mean)
So this is the code right now for getting all the information and show them in a table:
$selection = "SELECT * FROM kundenbestellungen, zahlart, produkte, user, status_bestellung, wohnsitz, kontodaten
WHERE b_zahlung_id = z_id
AND b_produkte_id = p_id
AND b_user_id = u_id
AND b_status_id = sb_id
AND w_user_id = u_id
AND d_user_id = u_id";
$sql = mysqli_query ($dblink, $selection) OR die (mysqli_error($dblink));
if (mysqli_num_rows ($sql) > 0) {
while ($row = mysqli_fetch_assoc($sql)) {
?>
<tr>
<td>
<?php /*Change the status to sent*/
if ($row['b_status_id'] == '0') {
echo $row['sb_status'];
?>
<form action="admin-bestellungen.php" method="POST">
<input type="hidden" name="id" value="<?php echo $row['b_id']?>">
<input type="submit" name="versenden" value="versenden">
</form>
<?php
} else {
echo $row['sb_status'];
}
?>
</td>
<td> <?php echo $row['b_nummer'];?></td>
<td><?php echo $row['u_vorname']." ".$row['u_nachname'];?></td>
<td><?php echo $row['p_produktname'];?></td>
<td><?php echo $row['b_menge_produkt'];?></td>
<td><?php echo $row['b_einzelpreis'];?></td>
<td><?php echo $row['z_art'];?></td>
<td><?php echo $row['b_zeitpunkt'];?></td>
</tr>
<?php
}
}
I'm really confused. I tried this below the $selection part, just to start with something:
$anzahl_bestellungen = "SELECT COUNT(DISTINCT b_nummer) AS nr FROM kundenbestellungen";
$anzahl_bestellungen = mysqli_query ($dblink, $anzahl_bestellungen) OR die (mysqli_error($dblink));
$bestell = mysqli_fetch_array($anzahl_bestellungen);
print_r($bestell['nr']);
and the code counts the amount of the different order numbers (8). But if I use it without COUNT, it shows only the first order number (80425) and also counts only 1 result and doesn't get the other 7 numbers.
$anzahl_bestellungen = "SELECT DISTINCT b_nummer FROM kundenbestellungen";
$anzahl_bestellungen = mysqli_query ($dblink, $anzahl_bestellungen) OR die (mysqli_error($dblink));
$bestell = mysqli_fetch_array($anzahl_bestellungen);
print_r($bestell['b_nummer']);
$b = count($bestell['b_nummer']);
echo "<br>".$b;
I also tried to work something out with GROUP, but then the code shows only one item for each order number.
I tried to work with a for-loop as well, but that didn't work out either.
I thought about a multidimensional array, but I wasn't able to think through that whole thing, I'm not very good at php / mysqli.
So I have no idea how to go on. Maybe you can help me. This is my first question, so please let me know if I need to be more specific or you need more code or anything.
thanks a lot!
I'm trying to create a form that retrieves data from a database and then allows me to add data to one column for multiple entries.
Every entry has an ID, a lot of other fields, and a category. I am trying to add these categories for every ID in the database using one form.
I came up with the solution below, but (of course)this only inserts the LAST entry in the form, because the variable ID is changed with every new row.
The form I have now shows me what I want to see, but it does not save it the way I need it to.
The question is, (how) can I make a form that has all entries in the database with a dropdown menu next to it,
lets me select the right category from the dropdown, and save it to the database?
The form:
$result = mysqli_query($con,"SELECT * FROM aw");
while($row = mysqli_fetch_array($result))
{
echo '<tr><td><input type="hidden" name="ID" value="'.$row[ID].'."> '.$row[ID].'</td><td>';
echo '
<select name="cat" onchange="this.form.submit()">
<option value="C1">category1</option>
<option value="C2"">category2</option>
</select></td></tr>
';
}
?>
<tr><td><input type="submit" title="SAVE" ></td></tr>
</form>
The insert.php
$sql="REPLACE INTO aw (ID,cat)
VALUES
('$_POST[ID]','$_POST[cat]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
?>
I changed my code according to Tom's answer and I now have the following:
This does print the values like they should be, but it still saves only the last entry into the database. I'm sure I must be missing something here..
$name = $_POST['ID'];
$category = $_POST['cat'];
foreach( $name as $key => $n ) {
$sql="REPLACE INTO aw (ID,cat)
VALUES
('$n','$category[$key]')";
print "The id is ".$n.", category is ".$category[$key]."<br>";
}
First of all, use PDO::Mysql, the SQL functions you are using are a bit deprecated and do not focus much on security. At the moment your code is vulnerable to SQL injections and your output is sensitive to XSS attacks (always sanitize output).
I was wrong, MySQL is deprecated but MySQLi is not! I do prefer using PDO::Mysql because of the range of databases it supports (MySQLi only supports a MySQL database, PDO::Mysql supports many more)
Now to your original question, you can create a sort of array. By making name="ID" to name="ID[]" and name="cat" to name="cat[]".
Now you can do
$name = $_POST['ID'];
$category = $_POST['cat'];
foreach( $name as $key => $n ) {
print "The id is ".$n.", category is ".$category[$key];
}
The problem is your using the name elements regardless of how many rows..
So name="ID" & name="cat" needs to change on each row or have an array type
you could use something like name="ID[]" as this would append/ create an array to $_POST['ID']... but you still would want to change your SQL query to handle each of these.
EDIT
If i understand, you want to be able to identify a row from the table so you can use that in the database?? One way todo this is when creating the table.. Give the TR a id/name attribute that is the row id from the database.
Then can simply know by checking that if your using the select menu from row #4, you check the id/name attribute of the current row and you have your database id.
<tr id='my_row_1'>
<td class='colName'>John</td>
<td class='colPhone'>1111</td>
<td class='colOther'>....</td>
</tr>
<tr id='my_row_2'>
<td class='colName'>Bill</td>
<td class='colPhone'>2222</td>
<td class='colOther'>....</td>
</tr>
<tr id='my_row_3'>
<td class='colName'>Roger</td>
<td class='colPhone'>3333</td>
<td class='colOther'>....</td>
</tr>
With something like the above, Say i had a button in on of the columns... When i click on that button.. all i have todo is find the parent TR and get its id value.... Then explode it by "_" and get the last piece to have the id..
So your PHP would generate the id easily... Also, using a form would not be the best case here.. Using multiple forms within a table is... wasteful... sort of ..
I would suggest more so, having a button that simple calls a js function which will then post/ajax/jquery what you need from that row.
--- Trying to understand exactly what you need??
My mysql table has 6 fields bkid bkname bkauth bkpub bkedn bkstock.
This program is just for testing you may see some extra lines which I have commented out because I am not using the commented lines for now.
Just for now I am trying to get bkid from the html form and then use it in a query to find out the last column of the retrieved result in $row as $row[5] which is books in stock bkstock. So,I need to find out the no of books in stock from the bkid provided by the user in the form and clicking the button to submit the form.
The query given below does not work.
Notice: Undefined offset: 5 in C:\xampp\htdocs\projects\library\incstockbook.php on line 41
<HTML>
<HEAD>
<h1 align="center">THIS PAGE ADDS STOCK OF BOOKS TO THE LIBRARY</h1>
</HEAD>
TO INCREASE THE STOCK OF BOOKS TO INCLUDE TO THE LIBRARY
<FORM action="incstockbook.php" method="POST">
<table>
<tr>
<td>ENTER THE BOOK ID :</td>
<td><input type=text name="bkid">
</TR>
<TR>
<td>ENTER THE NO. OF BOOKS TO INCLUDE TO THE LIBRARY:</td>
<td><input type=text name="bkstock">
</tr>
</table>
<BR>
CLICK HERE STOCK MORE BOOK :<input type="submit" value="ADD STOCK" name="submit"></br></br>
</FORM>
<?php
$server="localhost";
$username="root";
$password="pramit";
$db="test";
$mysqli = new mysqli($server,$username,$password);
if ($mysqli->errno)
{
printf("Unable to connect to the database:<br /> %s",
$mysqli->error);
exit();
}
$mysqli->select_db($db);
$query1 = "select bkstock from books where bkid=";
if(isset($_POST['submit']))
{
$bkid=$_POST['bkid'];
// $bkstock=$_POST['bkstock'];
$query1.="'$bkid'";
$result=$mysqli->query($query1,MYSQLI_STORE_RESULT);
$row = $result->fetch_array(MYSQLI_NUM);
echo "$row[5]";
}
$mysqli_close;
?>
First thing to do in such cases is to use var_dump(). It'll tell you what is in $row variable and allow you to fix that problem. And problem is the fact that you're trying to get sixth item from row when there is only one, so it should be $row[0].
But there are some more to fix here.
Check $mysqli_close; statement, maybe you wanted to use $mysqli->close()? Because like that it doesn't make any sense.
Next, never use raw user input data in queries. It's dangerous! You have to filter it, or better use prepared statements.
$row[5] tries to retrieve the 6th element from your array. Since this row maps to 1 row (the first) of your query result, the number of elements contained in the row is exactly the number of selected columns from your table. select bkstock from... indicates there will only be one element in your array, so only $row[0] will work.
And as an extra: there is no need to wrap it in a quote when you echo it. just echo $row[0]; should be fine.
I'm trying to get a row from the DB using php, i've made an html form that's supposed to take a book title from users and gets the review from the DB about this book, and then post it in an input text, the form's action leads to the following function :
function GetReview($BookTitle)
{
require'DB.php';
if(empty($_POST['BookTitle']))
{
echo " You must enter a book name!";
return false;
}
$BookTitle = mysql_real_escape_string($BookTitle);
$q="Select Reviews from Users_Booklist where (Book_Title like '%" .$BookTitle."%');";
if(!mysql_query($q,$con))
{
die("Error".mysql_error());
}
else
{
$row = mysql_fetch_row($q);
?>
<html>
<head><title>Delete Review </title>
</head>
<body>
<br>
<form name="DeleteReview " action="DeleteReviewsFunction.php" method="post">
Review: <input type="text" name="Review" size="200" value="<?php echo $row[0]; ?>"/>
<input type="submit" value="Delete Review" />
</form>
</body>
</html>
<?php
}
}
GetReview($_POST['BookTitle'])
However, it leads me to the next form with nothing in the input text and this warning:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\GetReview.php on line 20
I've searched and tried different code but still same result.
Could anyone please tell me where the error is???... Thanks
$qq = mysql_query($q,$con);
if(!$qq) {
// (...)
$row = mysql_fetch_row($qq);
I'm not going to be a lot of help, but your question seems to be where the error is occuring, and I can tell you that.
It's in the $row = mysql_fetch_row($q); line.
You can tell this because the error record starts with mysql_fetch_row(), and the above line is the only mention of mysql_fetch_row() in the code.
Check the SQL query by printing the output of $q variable with:
echo $q;
Now, try to execute it from your MySQL client. Collect the results (if there are) and check for errors.
A suggestion: If you want, you can use a tool like ezSQL that can be very useful (especially for code organization)