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);
Related
I have multiple checkbox in my form and the person need to input the quantity of the types of item that is selected. Now, my problem is that I can't get the data to be inserted into database.
This is my add_record.php code:
<?php
include("connect.php");
include("header.php");
$sql_student = "SELECT * FROM student";
$result_student = mysql_query($sql_student);
?>
<form method="post" id="add_form" action="add_record.php">
<label>Name</label>
<input placeholder="Enter Student Name" type="text" name="name" id="name" class="form-control" />
<br />
<input placeholder="Enter Student ID" type="text" name="stud_id" id="stud_id" class="form-control" />
<br />
<?php
$sql_baggage = "SELECT * FROM baggage";
$result_baggage = mysql_query($sql_baggage);
?>
<label>Bag Types</label></br>
<table style="border:none;">
<?php while($row_bag = mysql_fetch_array($result_baggage))
{
$baggage_id = $row_bag['baggage_id'];
?>
<tr>
<td><?php echo $row_bag['baggage_id'];?>
<td><?php echo $row_bag['baggage_type'];?></td>
<td><input type="checkbox" name="tick[]" value="<?php echo $baggage_id;?>"/></td>
<td><input type="text" size="2" name="txt[<?php echo $baggage_id;?>]" placeholder=" "></td>
<?php
?></td></tr>
</table>
<br />
<input type="submit" name="submit" id="submit" value="Add Record" class="btn btn-success btn-secondary pull-right" />
</form>
<?php
if(isset($_POST['submit']))
{
$name = $_POST["name"];
$stud_id = $_POST["stud_id"];
$stu_query = "INSERT INTO student(student_id,student_name) VALUES ('$stud_id','$name')";
if(mysql_query($stu_query))
{
if(!empty($_POST['tick']))
{
foreach($_POST['tick'] as $selected)
{
$qty = $_POST['txt'][$selected];
$inv_query = "INSERT INTO inventory (invstu_id,invbag_id,invbag_quantity) VALUES
('$stud_id','$selected', '$qty')";
if(mysql_query($inv_query))
{
echo'<script>alert("A record has been inserted!")</script>';
}
else
{
echo "Database error";
}
}
}
else
{
echo'<script>alert("A record has been inserted!")</script>';
}
}
}
?>
</body>
</html>
I know that the data is passed through foreach function since I get the echo of database error two times when I tick two of the checkbox. However, the value is not inserted into the database.
Finally solve the issue by echoing the mysql_error(), there is nothing wrong with the code. Just a bit problem at the database. Thanks!!
I'm fetching the data from database for a particular record using email id in the hidden field.
If I try to add as input type="text" it is not working for me, can any one check this.
Here is my code.
Index.php:
<?php
session_start();
$username = $_SESSION['username'];
if($username) {
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
} else {
echo "no";
}
?>
<html>
<body>
<table class="table table-hover">
<form action="personalinfo.php" METHOD="POST">
<input type="hidden" value="<?php echo $username;?>" name="email">
<tr>
<th><label for="first_name">Name</label></th>
</tr>
<?php
include "personalinfo.php";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
<input type="text" name="first_name" value="<?php echo $row->first_name;?>" />
echo "</tr>";
}
echo "</table>";
?>
</form>
</body>
</html>
personalinfo.php :
<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$result = mysql_query("SELECT * FROM registered where email='$username'");
?>
In index.php, if I use like this in while condition 'first_name';?> name="first_name"/> so that I can update the record but if i remove this
echo "<td>" . $row['first_name'] . "</td>";
and if I add like this instead of that echo
<?php echo $row->first_name;?>
it gives error as
Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\index\index.php on line 44
You have an error in your syntax try using this code
<?php
session_start();
?>
<?php
$username = $_SESSION['username'];
if($username) {
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
} else {
echo "no";
}
?>
<html>
<body>
<table class="table table-hover">
<form action="personalinfo.php" METHOD="POST">
<input type="hidden" value="<?php echo $username;?>" name="email">
<tr>
<th><label for="first_name">Name</label></th>
</tr>
<tr>
<?php
include "personalinfo.php";
while($row = mysql_fetch_array($result)) {
?>
<tr>
<input type="text" name="first_name" value="<?php echo $row->first_name;?>" />
</tr>
<?php }?>
</table>
</form>
</body>
</html>
You are getting error because there's a HTML tag in php code.
This line has issue, as it was written in PHP code:
<input type="text" name="first_name" value="<?php echo $row->first_name;?>" />
Try replacing the code with this :
<?php
include "personalinfo.php";
while($row = mysql_fetch_array($result)) {
?>
<tr>
<input type="text" name="first_name" value="<?php echo $row->first_name;?>" />
<!-- //or this can be used <input type="text" name="first_name" value="<?php echo $row->first_name;?>" />
-->
</tr>
<?php
}
?>
</table>
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.
I am quite new to this and this problem has been on my back for a few days now.
I have searched for answers to my problem but couldn't find anything satisfying for what I need.
I am trying to send some data through a form into a database and then represent the respective data into a table that shows specific rows.
My problem is that one of those rows (the Actions row) appears stored as blank data eventhough the user fills the form with something...anything.
This is my Add.php file:
<?php
$name="";$first="";$last="";$email="";$pass="";$acti="";
mysql_connect("localhost","admin") or die (mysqli_error());
$db=mysql_select_db("test") or die (mysql_error());
$con=mysqli_connect("localhost","admin","","test")or die (mysqli_error());
if (isset($_POST['save']))
{
$name=mysql_real_escape_string($_POST['name']);
$first=mysql_real_escape_string($_POST['first']);
$last=mysql_real_escape_string($_POST['last']);
$email=mysql_real_escape_string($_POST['email']);
$pass=mysql_real_escape_string($_POST['pass']);
if(isset($_POST['acti']){$actions=mysql_real_escape_string($_POST['acti']);}
echo "A new record has been added!";
$query="INSERT INTO users (Name,FirstName,LastName,Email,Password,Actions)
VALUES ('$name','$first','$last','$email','$pass','$acti')";
$result=mysql_query($query)
or die (mysql_error());
}
mysqli_close($con);
?>
//the form is included in the file
<html>
<head>
<body>
<br/><br/><h2 align=center ><b> Registration page </b></h2><br/><br/><br/>
<form enctype="multipart/form-data" method="POST" action="" align=center >
Name: <input type="text" name="name" value="<?php echo $name;?>"> <br/>
First Name:<input name="first" input type="text" value="<?php echo $first;?>"> <br/>
Last Name:<input name="last" input type="text" value="<?php echo $last;?>"> <br/>
Email: <input type="text" name="email" value="<?php echo $email;?>"> <br/>
Password:<input type="password" input name="pass" value="<?php echo $pass;?>"> <br/>
Actions:<input name="action" input type="text" value="<?php echo $acti;?>"><br/<br/>
<input type="submit" name="save" value="Save">
</form>
<p align=center>Click here for a list!</p>
</body>
</head>
</html>
And this is my List.php file:
<?php
mysql_connect("localhost","admin") or die (mysql_error());
mysql_select_db("test") or die (mysql_error());
/*mysql_query("CREATE TABLE users(id INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),Name VARCHAR(255),FirstName VARCHAR(255),LastName VARCHAR(255),Email VARCHAR(255),Password VARCHAR(255),Actions VARCHAR(255))") or die (mysql_error());*/
echo"<table border='2' align=center>";
echo"<tr> <th>Name</th> <th>Email</th> <th>Actions</th> </tr>";
$result=mysql_query("SELECT * FROM users")or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<tr> <td>";
echo $row['Name'];
echo"</td> <td>";
echo $row['Email'];
echo"</td> <td>";
echo $row['Actions'];
echo"</td> </tr>";
}
echo"</table>";
?>
Thank you in advance!
You're calling $_POST['acti']; when the field name is name='action'.
You've put:
if(isset($_POST['acti']){$actions=mysql_real_escape_string($_POST['acti']);}
^ acti ^acti
Your HTML:
Actions:<input name="action" input type="text" value="<?php echo $acti;?>"><br/<br/>
^action, not 'acti'
Either use name='acti' in your form, or $_POST['action'] in your PHP.
Please look at this link on how to use mysqli properly.
you are mixing between mysql and mysqli
switch all mysql codes to mysqli example:
$name=mysql_real_escape_string($_POST['name']);
should be
$name=mysqli_real_escape_string($_POST['name']);
this
$result=mysql_query($query)
or die (mysql_error());
should be
$result=mysqli_query($query)
or die (mysqli_error());
this
if(isset($_POST['acti'])
should be
if(isset($_POST['action'])
BTW: you connecting by two methodes ?? mysql and mysqli ??
remove this:
mysql_connect("localhost","admin") or die (mysqli_error());//this connection is mysql and is also wrong without password.
$db=mysql_select_db("test") or die (mysql_error());
and connect only by mysqli.
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>