Basic PHP System: View, Edit, Add, Delete records with MySQLi
#21
Posted 06 February 2012 - 02:18 PM
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#22
Posted 06 February 2012 - 07:38 PM
Ben, on 07 February 2012 - 03:18 AM, said:
I always get this 000 0000-00-00 00:00:000 format
but not this 000|0000-00-00 00:00:00|0 format
For example I type 100 2000-10-02 06:00:10 6 in the three textboxes,
100 -> textbox 1
2000-10-02 06:00:10 -> textbox 2
6 -> textbox 3
it still remain the same 000 0000-00-00 00:00:000.
I want to get this data 100|2000-10-02 06:00:10|6 in sql database.
Please help me to solve.
textbox codes found in insert.php
<!--one textbox submit--> <!--<tr>--> <!--<td>Schedule Time</td>--> <!--<td>:</td>--> <!--<td><input name="schedule_time" type="text" id="schedule_time"></td>--> <!--</tr>--> <!--three textboxes submission of schedule_time 000|0000-00-00 00:00:00|0 --> <td>Schedule Time</td> <td>:</td> <td> <input type="text" name="schedule_time" id="schedule_time" value="<?php echo $ary[0]; ?>"> <!--000--> <input type="text" name="schedule_time" id="schedule_time" value="<?php echo $ary[1]; ?>"> <!--0000-00-00 00:00:00--> <input type="text" name="schedule_time" id="schedule_time" value="<?php echo $ary[2]; ?>"> <!--0--> </td> </tr>
insert_ac.php
// Get values from form
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$schedule_time = $_POST['schedule_time'];
$val = "000|0000-00-00 00:00:00|0";
$ary = explode("|", $val);
$a = $ary[0];
$b = $ary[1];
$c = $ary[2];
$abc = $a . " " . $b . $c;
// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, lastname, email, schedule_time)VALUES('$name', '$lastname', '$email', '$abc')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
// close connection
mysql_close();
?>
database file for import
-- -- Database: `1` -- -- -------------------------------------------------------- -- -- Table structure for table `test_mysql` -- CREATE TABLE IF NOT EXISTS `test_mysql` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` varchar(65) NOT NULL DEFAULT '', `lastname` varchar(65) NOT NULL DEFAULT '', `email` varchar(65) NOT NULL DEFAULT '', `schedule_time` varchar(25) CHARACTER SET utf8 NOT NULL DEFAULT '000|0000-00-00 00:00:00|0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; -- -- Dumping data for table `test_mysql` -- INSERT INTO `test_mysql` (`id`, `name`, `lastname`, `email`, `schedule_time`) VALUES (1, 'john', 'lere', 'yhh@hotmail.com', '000|0000-00-00 00:00:00|0'),
#23
Posted 28 February 2012 - 07:01 AM
Attached File(s)
-
records.php (6.1K)
Number of downloads: 406
#24
Posted 28 February 2012 - 10:18 AM
Ansir, on 28 February 2012 - 04:01 AM, said:
Can you specify what issues you are having? Are you getting any errors? What are you expecting, and what are you getting?
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#25
Posted 06 March 2012 - 01:17 PM
I'm new to php and I'm having some trouble getting your tutorial working.
the view-paginated screen isn't working, could be my initial php setup that is at fault. Checked phpinfo() and from what I can see everything is setup.
Here is a snippet from my log.
[06-Mar-2012 18:08:59] PHP Warning: include(connect-db.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\ttt\view-paginated.php on line 13
[06-Mar-2012 18:08:59] PHP Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'connect-db.php' for inclusion (include_path='.;C:\php5\pear') in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\ttt\view-paginated.php on line 13
[06-Mar-2012 18:08:59] PHP Notice: Undefined variable: mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\ttt\view-paginated.php on line 19
[06-Mar-2012 18:08:59] PHP Fatal error: Call to a member function query() on a non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\ttt\view-paginated.php on line 19
Edit - Fixed the problem now. An absolute rookie mistake.
#26
Posted 16 March 2012 - 02:19 AM
Great tutorial. I m using it right now for my project. Actually I replaced the text boxes of First and last name by TEXTAREA. Actually I want to input information like one page or more. Bt I am unable to do it. Please help me out. I edited Records.php to replace textboxes by textarea. I am able to insert small amonut of data like 2-3 line. But when I am trying insert more its not happening. I am not able to Insert and Edit the large amount of data. Please help me out If anyone knows how to do it. I attached the Edited records.php file. Other files are use as its is.
Attached File(s)
-
records.php (9.27K)
Number of downloads: 359
#27
Posted 16 March 2012 - 07:09 AM
CREATE TABLE `players` (
`id` int(11) NOT NULL auto_increment,
`firstname` varchar(32) NOT NULL,
`lastname` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
The "varchar(32)" indicates the type of the column in the database, and it's currently limited to 32 characters. I would suggest using PHPMyAdmin or similar to modify the database, changing those "varchar" columns to use a "text" type instead that doesn't have character limits.
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#28
Posted 04 April 2012 - 03:21 AM
Here i m uploading the code for inserting the record with date of current day So Enjoy @@@@
You can use attached files freely and insert ur data into mysql with the current date......
Attached File(s)
-
insert.php (1.38K)
Number of downloads: 202 -
insert_ac.php (993bytes)
Number of downloads: 379 -
table.php (1.43K)
Number of downloads: 388
#29
Posted 07 April 2012 - 01:04 PM
I am totally new to php & found this which has helped me greatly to understand it.Thanks.
I have used your MySQL first example in MySQL basic to see how it all works but am having issues with the "delete" & the "edit".
I was hoping you could help me with it?
The "delete" is just not doing anything when it is clicked.
<?php
// connect to the database
include('connect-db.php');
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['cust_no']) && is_numeric($_GET['cust_no']))
{
// get id value
$cust_no = $_GET['cust_no'];
// delete the entry
$result = mysql_query("DELETE FROM customer WHERE id=$cust_no")
or die(mysql_error());
// redirect back to the view page header("Location: view.php");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
header("Location: view.php");
}
?>
In the "edit.php" I am getting a:
Parse error: syntax error, unexpected T_ELSE on line 96, which would appear to be the last "else" in the code below.
(Hope I have laid this out right & in the proper order, if not, my appologies.
the table is: customer.
the elements of the table are: cust_no, name,address, phone_no.
<?php
// creates the edit record form
function renderForm($cust_no, $name, $address, $phone_no, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="cust_no" value="<?php echo $cust_no; ?>"/>
<div>
<p><strong>Customer No:</strong> <?php echo $cust_no; ?></p>
<strong>Name: *</strong> <input type="text" name="name" value="<?php echo $name; ?>"/><br/>
<strong>Address: *</strong> <input type="text" name="address" value="<?php echo $address; ?>"/><br/>
<strong>Phone No: *</strong> <input type="text" name="phone_no" value="<?php echo $phone_no; ?>"/><br/>
<p>* Required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['cust_no']))
{
// get form data, making sure it is valid
$cust_no = $_POST['cust_no'];
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
$phone_no = mysql_real_escape_string(htmlspecialchars($_POST['phone_no']));
// check that fields are filled in
if ($name == '' || $address == '' || $phone_no == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($cust_no, $name, $address, $phone_no, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE customer SET name='$name', address='$address', phone_no='$phone_no' WHERE cust_no='$cust_no'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checking that it is numeric/larger than 0) if (isset($_GET['cust_no']) && is_numeric($_GET['cust_no']) && $_GET['cust_no'] > 0)
{
// query db
$cust_no = $_GET['cust_no'];
$result = mysql_query("SELECT * FROM customer WHERE cust_no=$cust_no")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$name = $row['name'];
$address = $row['address'];
$phone_no = $row['phone_no'];
// show form
renderForm($cust_no, $name, $address, $phone_no, '');
}
else
// if no match, display result
{
echo "No results";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error';
}
}
?>If you can help with these issues, I would be very thankful.
#30
Posted 08 April 2012 - 11:12 AM
For the edit record, I think this line is the problem:
// get the 'id' value from the URL (if it exists), making sure that it is valid (checking that it is numeric/larger than 0) if (isset($_GET['cust_no']) && is_numeric($_GET['cust_no']) && $_GET['cust_no'] > 0)
Looks like it is one long line, and the "//" at the start of the line means that it is all commented out. You need to have the "if" statement on its own line, like this:
// get the 'id' value from the URL (if it exists), making sure that it is valid (checking that it is numeric/larger than 0) if (isset($_GET['cust_no']) && is_numeric($_GET['cust_no']) && $_GET['cust_no'] > 0)
also, I believe you have an extra, unnecessary "}" at the end of the file.
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#31
Posted 08 April 2012 - 01:57 PM
Thanks for getting back to me on this.
As requested here is my "view.php":
I have re-checked it against your original & it seems OK....but then again, what do I know.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/* VIEW.PHP
Displays all data from the customer table
*/
// connect to the database
include('connect-db.php');
// get results from database
$result = mysql_query("SELECT * FROM customer")
or die(mysql_error());
// display data in table
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Customer No</th> <th> Name</th> <th>Address</th> <th>Phone no</th><th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result ))
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['cust_no'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['address'] . '</td>';
echo '<td>' . $row['phone_no'] . '</td>';
echo '<td><a href="edit.php?cust_no=' . $row['cust_no']. '">Edit</a></td>';
echo '<td><a href="delete.php?cust_no=' . $row['cust_no'] . '">Delete</a></td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p><a href="new.php">Add a new record</a></p>
</body>
</html>
On the "edit.php" question.
I went over it again & did find that stupid mistake that you showed above regarding the "//"
Its working great now, thanks.
I have left in the last "}" at the end as if I took it out it left an eneven number throughout the document.
They are supposed to be even pairs ..aren't they?
Regards,
TomTom.
#32
Posted 08 April 2012 - 07:00 PM
Quote
They are supposed to be even pairs ..aren't they?
When I formatted the file to show the hierarchy of the code, I thought I noticed an extra "}". I'm guessing I was incorrect though.
I took another look at your delete page, and I think it may be deleting the record correctly, but simply not redirecting you back. Note this line:
// redirect back to the view page header("Location: view.php"); should be two lines:
// redirect back to the view page
header("Location: view.php");
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#33
Posted 09 April 2012 - 06:20 AM
Yes, again, a foolish mistake.
I also changed the "id" to "cust_no" in the following:
$result = mysql_query("DELETE FROM customer WHERE id=$cust_no")
It is working now.Thanks.
Much appreciated.
I have also been trying to put together a "select" query for my database where the user can decide on the criteria that they want to search the table by.
The table has shop_no,shop_ address, contact_no, contact_name.
Would check boxes be the best way to go ?
<form action="select.php" method="get"> Shop Address<input name="1" type="checkbox" value="shop_address" /> Contact No<input name="1" type="checkbox" value="contact_no" /> Contact name<input name="1" type="checkbox" value="weight"/> <input name="2" type="text" /> <input type="submit" value="Submit" /> </form>
Could this be used to tick one box & then enter text to narrow the query further?
Not sure where to go from there.
#34
Posted 09 April 2012 - 08:43 PM
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#35
Posted 10 April 2012 - 01:49 PM
Search database table by:
1)shop address.
2)contact no
3)contact name
and based on that have the information delivered in a table.
<form action="select.php" method="get"> Shop Address<input name="1" type="checkbox" value="shop_address" /> Contact No<input name="1" type="checkbox" value="contact_no" /> Contact name<input name="1" type="checkbox" value="contact_name"/> <input name="2" type="text" /> <input type="submit" value="Submit" /> </form>
Thought that checkbox's were good that the user could tick the desired checkbox (address) & then enter in the text area (location from address in the table)
& get the details echoed out from that.
Of course putting the php together for that is way beyond me at this stage.
Or is there an easier way that you know of to basically let the user choose the criteria & then search based on that criteria?
#36
Posted 24 April 2012 - 05:36 AM
I have a problem with the file edit.php
This is the error:
Parse error: syntax error, unexpected $end in /opt/lampp/htdocs/server/GestionIp/edit.php on line 116
this is the code :
<?php
/* EDIT.PHP
Allows user to edit specific entry in database
*/
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($ID, $IP, $NOMBRE, $DESC, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $IP; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $ID; ?></p>
<p><strong>IP:</strong> <?php echo $IP; ?></p>
<strong>Nombre: </strong> <input type="text" name="NOMBRE" value="<?php echo $NOMBRE; ?>"/><br/>
<strong>DESC: </strong> <input type="text" name="DESC" value="<?php echo $DESC; ?>"/><br/>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['ID']))
{
// get form data, making sure it is valid
$ID = $_POST['ID'];
$NOMBRE = mysql_real_escape_string(htmlspecialchars($_POST['NOMBRE']));
$DESC = mysql_real_escape_string(htmlspecialchars($_POST['DESC']));
// check that NOMBRE/DESC fields are both filled in
if ($NOMBRE == '' || $DESC == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($IP, $ID, $NOMBRE, $DESC, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE players SET NOMBRE='$NOMBRE', DESC='$DESC' WHERE IP='$ID'")
or die(mysql_error());
// once saved, redirect back to the view page header("Location: enviar.php");
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'ID' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['$ID']) && is_numeric($_GET['$ID']) && $_GET['$ID'] > 0)
{
// query db
$ID = $_GET['$ID'];
$result = mysql_query("SELECT * FROM players WHERE $ID=$ID")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'IP' matches up with a row in the databse
if($row)
{
// get data from db
$NOMBRE = $row['NOMBRE'];
$DESC = $row['DESC'];
// show form
renderForm($ID, $IP, $NOMBRE, $DESC, '');
}
else
// if no match, display result
{ echo "No results!";
}
}
else
// if the 'IP' in the URL isn't valid, or if there is no 'IP' value, display an error
{
echo 'Error!';
}
}
?>
I don't know what happen because I copy your code and replaced the specific code for my configuration.
Thanks for all
Bye
PD: One question , What does it do the function renderform()?;
#37
Posted 01 November 2012 - 06:57 PM
#38
Posted 12 January 2013 - 06:33 PM
#39
Posted 07 February 2013 - 04:14 AM
I have been looking for a practical mysqli tutorial all over the internet I couldn't find one like yours.
I am so glad to have found your tutorial here.
Please, can you find time to also code a sample to query from a html form with multiple inputs?
Stay blessed.
joseph
#40
Posted 07 February 2013 - 12:34 PM
I am trying to output an image stored from a directory (it's name save in mysql database) as follows:
<img src=\"./images/$row[15]\" width=\"90\" height=\"120\" alt=\"\" />
But I am only getting \"\" printed instead of an image itself.
Please, help.
joseph

Help












