mysqli_num_rows() expects parameter 1 to be mysqli_result, null given - php

receiving this error:
mysqli_num_rows() expects parameter 1 to be mysqli_result, null given
Not sure why and have browsed a lot of topics on here trying to figure it out. I know it has something to do with my query, but I just can't put the pieces together.
Thanks for the assistance, I appreciate it!
<?php
session_start();
if (!isset($_SESSION['email'])){
echo "Please login first.";
exit();
}else{
include ("header.php");
require_once ('mysqli_connect.php');
$id=$_GET['id'];
$query = "SELECT * FROM post WHERE post_id=$id";
$result = #mysqli_query ($query);
$num = mysqli_num_rows($result);
if ($num > 0) {
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
?>
<form action="update.php" method="post">
<p>Title: <input name="title" size=50 value="<? echo $row['title']; ?>"></p>
<p>Author: <input name="author" size=50 value="<? echo $row['author']; ?>"></p>
<p>Post: <input name="post" size=50 value="<? echo $row['post']; ?>"></p>
<p>
<input type=submit value=update>
<input type=reset value=reset>
<input type=hidden name="post_id" value="<? echo $row['post_id']; ? >">
</form>
<?
}
}
}
?>

In the line $result = #mysqli_query ($query); the # will silence any errors. Check that $result is not false, null, or some other error before proceeding.
You may want to go without the # until you have it working so you can get more meaningful error messages.

The first thing is that in the last section the code is not properly intented..like value="<? echo $row['post_id']; ?..Then secondly as Joe said the # symbol supress errors..So you need to remove those #symbol from your code..So the code must be ..
<?php
session_start();
if (!isset($_SESSION['email'])){
echo "Please login first.";
exit();
}else{
include ("header.php");
require_once ('mysqli_connect.php');
$id=$_GET['id'];
$query = "SELECT * FROM post WHERE post_id=$id";
$result = mysqli_query ($query);
$num = mysqli_num_rows($result);
if ($num > 0) {
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
?>
<form action="update.php" method="post">
<p>Title: <input name="title" size=50 value="<? echo $row['title']; ?>"></p>
<p>Author: <input name="author" size=50 value="<? echo $row['author']; ?>"></p>
<p>Post: <input name="post" size=50 value="<? echo $row['post']; ?>"></p>
<p>
<input type=submit value=update>
<input type=reset value=reset>
<input type=hidden name="post_id" value="<? echo $row['post_id']; ?>">
</form>
Hope this one helps you ..If any problems regarding then comment below ..:)

I think you must check the value of GET['id'] is having value or not and query is formed with appropriate data.
You might be getting the error because the result is null.
and also check the connection.

Related

building an evaluation form php

I'm trying to create an evaluation form for students, all questions are stored in db. I'm retrieving them from db any trying to store the answers for each question on db again. all answers should be selected from radio buttons as the following:
<?php
$sql = "SELECT Q_body, Q_ID FROM s_evaluation_questions WHERE Ev_ID='1'";
$result = $conn->query($sql);
?>
<?php
if ($result->num_rows > 0)
{
?>
<form action="AnswerS.php" method="POST">
<table align="right" id="keywords" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th colspan="4"> </th>
<th>Question</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc())
{ $answer="s".$row["Q_ID"];
?>
<tr>
<td>
<input type="hidden" name="Q_ID" value="<?php echo $row["Q_ID"]; ?>" >
<input type="radio" name=<?php echo $answer?> value="Bad" >Bad
<input type="radio" name=<?php echo $answer?> value="Good"> Good
<input type="radio" name=<?php echo $answer?> value="VeryGood"> Very Good
<input type="radio" name=<?php echo $answer?> value="Excellent"> Excellent</td>
<td align="right"> <?php echo $row["Q_body"]?></td>
</tr>
<?php } ?>
</tbody>
</table></br></br></br></br></br></br></br>
<input type="submit" value="Send" />
</form>
<?php
?></div>
<?php
} else
{echo "not allowed";}
And on the AnswerS.php page I suppose to store the answers on db as the following:
$UID='1';
$answer=$_POST['answer'];
$Q_ID= $_POST['Q_ID'];
$sql = "INSERT INTO s_evaluation_answers(Q_ID, A_body, UID) VALUES($Q_ID, $answer, $UID) ";
$result = $conn->query($sql);
if ($result === TRUE) {
echo "done" ;
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
but unfortunately it doesn't work!, I tried to trace the value of Q_ID and it gives only the last question id not all questions.
how can I store the selected value from the radio button for each question and store it as the answer for this question? (note: all questions are brought from db)
thanks
If you see your source html you will find that you have many fields with name Q_ID:
<input type="hidden" name="Q_ID" value="one id" >
<input type="hidden" name="Q_ID" value="another id" >
<input type="hidden" name="Q_ID" value="some more id" >
So browser sends you the last value of Q_ID - in above case it is some more id.
To avoid this - use [] notation in name attribute:
<input type="hidden" name="Q_ID[]" value="one id" >
<input type="hidden" name="Q_ID[]" value="another id" >
<input type="hidden" name="Q_ID[]" value="some more id" >
After that, outputting $_POST['Q_ID'] will give you an array. You can iterate over it with foreach and gather other info you need. Something like (simplified):
foreach ($_POST['Q_ID'] as $q) {
// answer will be stored in a `s . $q` value of POST
$answer = $_POST['s' . $q];
$sql = "INSERT INTO s_evaluation_answers(Q_ID, A_body, UID) VALUES($q, $answer, $UID) ";
$result = $conn->query($sql);
}

invalid argument foreach() in PHP

I have a shopping cart with a foreach() to update the quantities.
HTML
<form method="post" >
<?php while ($data2 = mysql_fetch_array( $data)) { ?>
<tr>
<td> <input name="quantity" type="text" class="form-field" size="3" value="<? echo $data2['quantity'];?>" />
<input type="hidden" name="product_id" value="<? echo $data2['product_id']; ?>">
</td>
</tr>
<? } ?>
</form>
AND FOR MY PHP, I have
if (isset($_POST['submit_qty']))
{
foreach($_POST['product_id'] as $key => $id)
{
$item_id = $id;
$quantity = $_POST['quantity'][$key];
$sql2 = "update cart SET quantity = '".$quantity."' where product_id = '".$item_id."' ";
$result2 = mysql_query($sql2) or die ("Error in query: $result2");
}
header('Location: mycart.php');
exit();
}
IF I Echo my sql query I get :
Warning: Invalid argument supplied for foreach()
what could be the problem ?
You aren't passing your product_id as an array.
Try <input type="hidden" name="product_id[]" value="<? echo $data2['product_id']; ?>">

How to send multiple row value submit button?

I get the values from same row, but many values with fetch array. I did it, but with buttons, as many as how many values there are. If I have 5 rows, then I should have five submits, but I want one submit button.
Here is my code:
$result2 = mysql_query ("select * from price where dom='$cat'",$db);
$myrow2= mysql_fetch_array($result2);
<form action="priceupdatetes.php" method="post">
<?php
do {
echo <<<here
<td><input name="etiket[$myrow2[id]]" type="text" value="$myrow2[etiket]"/></td>
<td><input name="pricestandart[$myrow2[id]]" type="text" value="$myrow2[pricestandart]"/></td>
<td><input name="number[$myrow2[id]]" type="text" value="$myrow2[number]"/></td>
<td><input name="totalunper[$myrow2[id]]" type="text" value="$myrow2[totalunper]" disabled="disabled"/></td>
<td><input name="discount[$myrow2[id]]" type="text" value="$myrow2[discount]"/></td>
<td><input name="totalwithper[$myrow2[id]]" type="text" value="$myrow2[totalwithper]" disabled="disabled"/></td>
</tr>
here;
}
while($myrow2= mysql_fetch_array($result2)) ;
?>
<input NAME="id[]" TYPE=hidden value="<?php foreach($myrow2[id] as $mid) {print $mid;} ?> "/>
<input name="submit" type="submit" value="Submit"/><br>
</form>
HERE is UPDATEPAGE.php:
if (isset($_POST['etiket'])) {$etiket = $_POST['etiket']; }
if (isset($_POST['pricestandart'])) {$pricestandart = $_POST['pricestandart'];}
if (isset($_POST['number'])) {$number = $_POST['number']; }
if (isset($_POST['discount'])) {$discount = $_POST['discount']; }
if (isset($_POST['id'])) {$id = $_POST['id']; }
$totalunper=$pricestandart*$number;
$percent=$discount/100;
$totalwithper1=$totalunper*$percent;
$totalwithper=$totalunper-$totalwithper1;
foreach($id as $team_id)
{
$result = mysql_query("UPDATE price SET etiket='$etiket[$team_id]',pricestandart='$pricestandart[$team_id]',number='$number[$team_id]',totalunper='$totalunper[$team_id]',discount='$discount[$team_id]',totalwithper='$totalwithper[$team_id]' WHERE id='$team_id'"); }
How can I get values with different id's and update them?
<input NAME="id[]" TYPE=hidden value="<?php foreach($myrow2[id] as $mid) {print $mid;} ?> "/>
is wrong. $myrow2[id] is not an array. Inside your while-loop, you should add:
<input NAME="id[]" TYPE="hidden" value="$myrow2[id]"/>

Editing data from MySQL via PHP

I am running into a frustrating problem with a PHP script that's supposed to allow me to edit individual rows within my MySQL database.
This is the file where all of the rows from the database are displayed; it works just like it's supposed to.
<table cellpadding="10">
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>E-mail</td>
<td>Phone</td>
</tr>
<?php
$username="username here";
$password="password here";
$database="database name here";
mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM students";
$result=mysql_query($query);
mysql_close();
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[id]</td>");
echo ("<td>$row[first]</td>");
echo ("<td>$row[last]</td>");
echo ("<td>$row[email]</td>");
echo ("<td>$row[phone]</td>");
echo ("<td>Edit</td></tr>");
}
echo "</table>";
?>
As you can see, each row has an "Edit" link that is supposed to allow the user to edit that individual student's data. Here, then, is StudentEdit.php:
<?php
$username="username";
$password="password";
$database="database";
mysql_connect(localhost,$username,$password);
$student_id = $_GET[id];
$query = "SELECT * FROM students WHERE id = '$student_id'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
mysql_close();
?>
<form method="post" action="EditStudentData.php" />
<table>
<tr>
<td><input type="hidden" name="id" value="<? echo "$row[id]" ?>"></td>
</tr>
<tr>
<td>First Name:</td>
<td><input type="text" name="first" value="<? echo "$row[first]" ?>"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="last" value="<? echo "$row[last]" ?>"></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="phone" value="<? echo "$row[phone]" ?>"></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" value="<?echo "$row[email]" ?>"></td>
</tr>
</table>
</form>
When I execute this, however, I get the following error message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home4/lukaspl1/public_html/StudentEdit.php on line 12
Any ideas what's wrong, and how to fix it?
Thank you in advance!
Remove the mysql_close from here
mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM students";
$result=mysql_query($query);
mysql_close();
The code should mysql_connect(localhost,$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM students";
$result=mysql_query($query);
And moreover,you are going to use only key based resultset.. simply have mysql_fetch_assoc.
And another suggestion would be instead of $row[id]..replace it with $row['id'].
StudentEdit.php: you forgot to call #mysql_select_db($database) or die( "Unable to select database"); before you executed the query
This part of the code is wrong:
$student_id = $_GET[id];
the correct code is
$student_id = $_GET['id'];
code from expertsnote.com
Try...
echo ("<td>Edit</td></tr>");
instead of
echo ("<td>Edit</td></tr>");
this code was missing
$select_db = mysql_select_db("$db_name");
if (!$select_db) {echo "Error Selecting Database";}
this is the cod for edit the details dynamically
<?php
include('db.php');
$id=$_REQUEST['id'];
$query="SELECT * FROM `camera details` WHERE id='".$id."'";
$result=mysqli_query($db,$query) or die(mysqli_error());
$row1=mysqli_fetch_assoc($result);
if(isset($_POST['submit'])&&(isset($_POST['new'])&&($_POST['new'])==1))
{
$id=$_REQUEST['id'];
foreach($_POST as $key=>$values)
{
if($key!="submit"){
$names[]=$key;
$val[]= "'".$values."'";
if($key!="new"){
$k[] = "`".$key."` = '".$values."'";
}
}
}
$output=implode(",",(array)($k));
//$v=implode(",",(array)($val));
// `name` = 'san'
$query="UPDATE `camera details` SET $output WHERE id='".$id."'";
$output=mysqli_query($db,$query) or die(mysqli_error($db));
if($output)
{
header('location:cameralist.php');
}
}
else{
?>
I recommend doing this in studentEdit.php
$student_id = mysql_real_escape_string($_GET[id]);
$query = "SELECT * FROM students WHERE id = '$student_id'";
$result = mysql_query($query) or die(mysql_error() . ' ' . $query);
$row = mysql_fetch_array($result);
mysql_close();
Two things I've changed here is firstly to escape the data being passed in the url and secondly I've added or die(mysql_error() . ' ' . $query); If something is going wrong in the sql statement you should now see the error and hopefully you'll be able to fix it from there.
What looks incorrect to me is the way you are displaying the value retrieved from the database:
<input type="hidden" name="id" value="<? echo "$row[id]" ?>">
It should be
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
This code gives the option to add, search, edit and delete options. Thought it might to see all the options in one code.
$searchedUsername = "";
$searchedEmail = "";
//registration (Add) function
if ( isset($_POST['stdregister'])){
$username = $_POST['stdusername'];
$password = $_POST['stdpassword'];
$email = $_POST['stdemail'];
$hashedPassword = md5($password);
$connection = mysqli_connect("localhost","root","","std");
$query = "INSERT INTO student VALUES ('$username','$hashedPassword','$email')";
if ( mysqli_query($connection,$query) == 1 ){
echo "Successfully saved";
}
else{
echo "<p style='color: #f00;'>There is an error</p>";
}
mysqli_close($connection);
}
//delete function
if ( isset($_POST['stddelete'])){
$username = $_POST['stddelusername'];
$connection = mysqli_connect("localhost","root","","std");
$query = "DELETE FROM student WHERE username LIKE '$username'";
mysqli_query($connection,$query);
echo mysqli_error($connection);
mysqli_close($connection);
}
//update function
if ( isset($_POST['stdupdate'])){
$username = $_POST['stdusername'];
$stdpass = md5($_POST['stdpassword']);
$stdemail = $_POST['stdemail'];
$connection = mysqli_connect("localhost","root","","std");
$query = "UPDATE student SET password='$stdpass', email='$stdemail' WHERE username LIKE '$username'";
mysqli_query($connection,$query);
echo mysqli_error($connection);
mysqli_close($connection);
}
if ( isset($_POST['stdsearch']) ){
$searchUsername = $_POST['stdeditusername'];
$connection = mysqli_connect("localhost","root","","std");
$query = "SELECT * FROM student WHERE username LIKE '$searchUsername' ";
$result = mysqli_query($connection, $query);
while( $row = mysqli_fetch_array($result) ){
$searchedUsername = $row['username'];
$searchedEmail = $row['email'];
}
}
?>
<html>
<head>
</head>
<body>
<h1>Student Registration</h1>
<form name="stdregistration" action="forms.php" method="post">
<label>Username :</label>
<input name="stdusername" required="required" type="text" /><br /><br />
<label>Password :</label>
<input name="stdpassword" type="password" /><br /><br />
<label>E-mail :</label>
<input name="stdemail" type="email" /><br /><br />
<input name="stdregister" type="submit" value="Save" />
</form>
<h2>Delete Students</h2>
<form name="stddeletion" action="forms.php" method="post">
<label>Select the Username :</label>
<select name="stddelusername" required>
<option value="">Select One</option>
<?php
$connection2 = mysqli_connect("localhost","root","","std");
$query2 = "SELECT username FROM student";
$result = mysqli_query($connection2,$query2);
while( $row = mysqli_fetch_array($result) ){
echo "<option value='".$row['username']."'>".$row['username']."</option>";
}
mysqli_close($connection2);
?>
</select>
<input name="stddelete" type="submit" value="Delete" />
</form>
<h2>Edit Students</h2>
<form name="stdedition" action="forms.php" method="post">
<label>Select the Username :</label>
<select name="stdeditusername" required>
<option value="">Select One</option>
<?php
$connection2 = mysqli_connect("localhost","root","","std");
$query2 = "SELECT username FROM student";
$result = mysqli_query($connection2,$query2);
while( $row = mysqli_fetch_array($result) ){
echo "<option value='".$row['username']."'>".$row['username']."</option>";
}
mysqli_close($connection2);
?>
</select>
<input name="stdsearch" type="submit" value="Search" />
</form>
<form name="stdedit" action="forms.php" method="post">
<label>Username :</label>
<input name="stdusername" required="required" type="text" readonly value="<?php echo $searchedUsername; ?>" /><br /><br />
<label>Password :</label>
<input name="stdpassword" type="password" /><br /><br />
<label>E-mail :</label>
<input name="stdemail" type="email" value="<?php echo $searchedEmail; ?>" /><br /><br />
<input name="stdupdate" type="submit" value="Update" />
</form>
</body>
</html>

Database form does display results

I am having trouble displaying my results in the form. Could anyone show me what I am doing wrong? The only thing that is showing up is the echo Database Output. I am trying to create a database to update a webpage. I am suppose to go to the admin page which contains this form and should be able to add, delete and update the webpage any suggestions would help. Thanks in advance.
</head>
<body>
<?php
$id=$_POST['id'];
$db="";
$link = mysql_connect('localhost', '', '');
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
$query=" SELECT * FROM tblContent WHERE PageID ='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$pageHeading=mysql_result($result,$i,"PageHeading");
$subHeading=mysql_result($result,$i,"SubHeading");
$contentTxt=mysql_result($result,$i,"Content");
$pageTitle=mysql_result($result,$i,"PageTitle");
$metaDescription=mysql_result($result,$i,"MetaDescription");
$metaKeywords=mysql_result($result,$i,"MetaKeywords");
?>
<form method="post" action="admin.php">
<input type="hidden" name="ud_id value="<? echo $id; ?>">
LinkText: <input type="text" name="ud_LinkText" value="<? echo $contectTxt; ?>"><br>
Page Heading:<input type="text" name="ud_PageHeading" value="<? echo $id; ?>">
Sub Heading:<input type="text" name="ud_SubHeading"
value="<? echo $subHeading; ?>"><br>
Page Title: <input type="text" name="ud_PageTitle" value="<? echo $pageTitle; ?>"><br>
MetaDescription: <input type="text" name="ud_MetaDescription"
value="<? echo $metaDescription; ?>"><br>
MetaKeywords: <input type="text" name="ud_MetaKeywords"
value="<? echo $metaKeywords; ?>"><br>
<input type="Submit" value="Update">
</form>
<?php
++$i;
}
?>
</body>
</html>
$db=""; //add your database name here
You probably get an error message due to this mis spelled function:
$num=mysql_numrows($result);
should be
$num=mysql_num_rows($result);

Categories