Basic PHP System: View/Edit/Delete/Add Records
#31
Posted 10 November 2010 - 12:20 PM
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#32 Guest_Zaas
Posted 09 December 2010 - 03:31 PM
Mark
#33
Posted 09 December 2010 - 06:21 PM
Zaas, on 09 December 2010 - 12:31 PM, said:
Interesting. So you provide it with MySQL host/username/password/database name, and it automatically finds all of your tables/columns?
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#34
Posted 05 May 2011 - 03:35 AM
Script:
<?php
/*
NEW.PHP
Allows user to create a new entry in the database
*/
// creates the new record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($order, $discrjob, $item, $status, $daytimereq, $contact, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>New 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">
<div>
<strong>Order: *</strong> <input type="text" name="order" value="<?php echo $order; ?>" /><br/>
<strong>Discrete job: *</strong> <input type="text" name="discrjob" value="<?php echo $discrjob; ?>" /><br/>
<strong>Item: *</strong> <input type="text" name="item" value="<?php echo $item; ?>" /><br/>
<strong>Status: *</strong> <input type="text" name="status" value="<?php echo $status; ?>" /><br/>
<strong>Day&time request: *</strong> <input type="text" name="daytimereq" value="<?php echo $daytimereq; ?>" /><br/>
<strong>Contact: *</strong> <input type="text" name="contact" value="<?php echo $contact; ?>" /><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, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$order = mysql_real_escape_string(htmlspecialchars($_POST['order']));
$discrjob = mysql_real_escape_string(htmlspecialchars($_POST['discrjob']));
$item = mysql_real_escape_string(htmlspecialchars($_POST['item']));
$status = mysql_real_escape_string(htmlspecialchars($_POST['status']));
$daytimereq = mysql_real_escape_string(htmlspecialchars($_POST['daytimereq']));
$contact = mysql_real_escape_string(htmlspecialchars($_POST['contact']));
// check to make sure both fields are entered
if ($order == '' || $discrjob == '' || $item == '' || $status == '' || $daytimereq == '' || $contact == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($order, $discrjob, $item, $status, $daytimereq, $contact, $error);
}
else
{
// save the data to the database
mysql_query("INSERT qcdata SET order='$order', discrjob='$discrjob', item='$item', status='$status', daytimereq='$daytimereq', contact='$contact'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','','','');
}
?>
ERROR:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order='order1', discrjob='discrjob1', item='item1', status='status1', daytimereq' at line 1
#35
Posted 05 May 2011 - 08:19 AM
I believe that "order" is a reserved word within MySQL (it's used in "ORDER BY"), which can lead to issues if you try to use it as a column name. I would change that column name to something other than "order" ("sort" or "sortOrder"?) and update your mysql query. My guess is that will fix it, since I'm not seeing anything else wrong with the query at first glance.
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#36
Posted 06 May 2011 - 04:44 AM
Ben, on 05 May 2011 - 02:19 PM, said:
I believe that "order" is a reserved word within MySQL (it's used in "ORDER BY"), which can lead to issues if you try to use it as a column name. I would change that column name to something other than "order" ("sort" or "sortOrder"?) and update your mysql query. My guess is that will fix it, since I'm not seeing anything else wrong with the query at first glance.
You're totally right Ben... this did the trick. Thanks very much!
#37
Posted 10 May 2011 - 01:52 AM
#38 Guest_George Mpopo
Posted 10 May 2011 - 02:16 AM
#39 Guest_rrrnew
Posted 12 May 2011 - 05:08 PM
administrator, on 29 April 2010 - 01:37 PM, said:
I decided to back the Zend Framework thought because I figured it would become the predominant PHP framework over time - as it has.
Stefan
Hi im new to php in im having problem editing a db. I wrote down a code but it doesn't update the db. This is the code that i wrote:
<?php
include('mysql_connection.php');
include('../includes/header.php');
mysql_select_db($database_connection) or die ("unable to connect to the database");
$id=$_POST['id'];
?>
<?php
$dbc=NULL;
//Verifica que los encasillado en la pgina de editar esten llenos
if (empty($_POST['direccion_fisica'])) {
$errors[]='No entrastes la direccion fisica.';
}else{
$df=mysqli_real_escape_string($dbc, trim($_POST['direccion_fisica']));
}
if (empty($_POST['zip'])) {
$errors[]='No entrastes el codigo postal.';
}else{
$zp=mysqli_real_escape_string($dbc, trim($_POST['zip']));
}
if (empty($_POST['direccion_postal'])) {
$errors[]='No entrastes la dirección postal.';
}else{
$dp=mysqli_real_escape_string($dbc, trim($_POST['direccion_postal']));
}
if (empty($_POST['zip_ps'])) {
$errors[]='No entrastes el codigo postal.';
}else{
$zp_ps=mysqli_real_escape_string($dbc, trim($_POST['zip_ps']));
}
if (empty($_POST['telefono_1'])) {
$errors[]='No entrastes el telefono.';
}else{
$tl_1=mysqli_real_escape_string($dbc, trim($_POST['telefono_1']));
}
if (empty($_POST['telefono_2'])) {
$errors[]='No entrastes el telefono.';
}else{
$tl_2=mysqli_real_escape_string($dbc, trim($_POST['telefono_2']));
}
if (empty($_POST['fax'])) {
$errors[]='No entrastes el fax.';
}else{
$fx=mysqli_real_escape_string($dbc, trim($_POST['fax']));
}
if (empty($_POST['web_page'])) {
$errors[]='No entrastes tu pagina de internet.';
}else{
$wb=mysqli_real_escape_string($dbc, trim($_POST['web_page']));
}
if (empty($_POST['nombre'])) {
$errors[]='No entrastes el nombre del contacto.';
}else{
$fn=mysqli_real_escape_string($dbc, trim($_POST['nombre']));
}
if (empty($_POST['apellido'])) {
$errors[]='No entrastes el apellido del contacto.';
}else{
$ln=mysqli_real_escape_string($dbc, trim($_POST['apellido']));
}
if (empty($_POST['nombre_cs'])) {
$errors[]='No entrastes el nombre del contacto secundario.';
}else{
$fn_2=mysqli_real_escape_string($dbc, trim($_POST['nombre_cs']));
}
if (empty($_POST['apellido_cs'])) {
$errors[]='No entrastes el apellido del contacto secundario.';
}else{
$ln_2=mysqli_real_escape_string($dbc, trim($_POST['apellido_cs']));
}
if (empty($_POST['email'])) {
$errors[]='No entrastes el correo electronico.';
}else{
$em=mysqli_real_escape_string($dbc, trim($_POST['email']));
}
if (empty($_POST['email_sec'])) {
$errors[]='No entrastes el correo electronico del contacto secundario.';
}else{
$em_2=mysqli_real_escape_string($dbc, trim($_POST['email_sec']));
}
if (empty($_POST['telefono'])) {
$errors[]='No entrastes el telefono del contacto.';
}else{
$tl_3=mysqli_real_escape_string($dbc, trim($_POST['telefono']));
}
if (empty($_POST['phone_sec'])) {
$errors[]='No entrastes el telefono del contacto secundario.';
}else{
$tl_4=mysqli_real_escape_string($dbc, trim($_POST['phone_sec']));
}
if (empty($errors)) { //Si todo esta bien
$q="UPDATE perfil_negocio_agricola SET direccion_fisica='$fn', zip='$zp', direccion_postal='$dp', zip_ps='$zp_ps', telefono_1='$tl_1', telefono_2='$tl_2', fax='$fx', web_page='$wb', nombre='$fn', apellido='$ln', nombre_cs='$fn_2', apellido_cs='$ln_2', email='$em', email_sec='$em_2', telefono='$tl_3', phone_sec='$tl_4', WHERE id='$id' ";
$r=@mysqli_query($dbc, $q);
if(mysqli_affected_rows($dbc) ==0)
{//Si corrio OK
echo'<p> Su perfil a sido editado.</p>';
}else{
echo'<p> class="error">Su perfil no pudo ser editado. Disculpenos por cualquier inconveniento que esto pueda causar.</p>';
echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; //Debuggin MSG
}
}else{ //Reportar errores
echo'<p class="error">A ocurrido los siguientes errores:<br/>';
foreach($errors as $msg) {
echo "- $msg<br/>\n";
}
echo'</p><p>Intente de nuevo.</p>';
} //Finaliza sino hay errores
mysqli_close($dbc);
?>
I get this error mysqli_real_escape_string() expects parameter 1 to be mysqli, null given , i have look the code again to see if wrote something wrong but i dont found it. Since i start like 2 weeks ago with PHP if someone can help me it would be great.
#40
Posted 14 May 2011 - 04:43 PM
If you check the documentation for mysqli_real_escape_string (http://php.net/manua...ape-string.php), you'll notice that it is expecting two arguments: a link to the mysqli connection, and the string you want escaped:
string mysqli_real_escape_string ( mysqli $link , string $escapestr )
In your code above, you call this function like this:
$df=mysqli_real_escape_string($dbc, trim($_POST['direccion_fisica']));
but just a couple lines above, you have set $dbc to NULL:
$dbc=NULL; //Verifica que los encasillado en la pgina de editar esten llenos
Why are you setting the variable to null? The error message indicates that it is expecting the first variable to be a link to the mysqli connection, but you are providing it with null instead.
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#41 Guest_udai
Posted 24 May 2011 - 01:55 AM
some one help me..........
#42
Posted 24 May 2011 - 05:11 AM
Thanks for the great tips i always love to learn new things. I have little bit knowledge about programming languages. I hope i will learn a lot from here as This is the time to learn some PHP.
#43
Posted 24 June 2011 - 11:39 AM
This may be beyond the scope of the tutorial, but I have seen it done a lot in tutorials a friend of mine looks at to learn PHP:
$db = mysql_connect('localhost','wronguser','or_wrongpass') or die (mysql_error());
Now, with my wamp configuration (Pretty much default) and my hosting configuration (through a2 hosting) I get (as expected) the MySQL error printed out twice:
> Once for the actual PHP error (due to what the error level is set to
> And again for the or die (mysql_error());, except this time it's just the error reported from mysql.
As such I have always done
$db = @mysql_connect('localhost','wronguser','or_wrongpass') or die ('Could not connect to the DB');
Utilizing the @ error control operator to ignore the PHP error for this statement and then removing the mysql_error (I don't want my database user printed out).
I am not sure if this is best practice or not, since (to my understanding) @ simply disables error reporting then executes the statement, and returns it to normal.
http://us3.php.net/m...rrorcontrol.php
If anyone wants to read up more on it, and doesn't already know about it.
So my question, is there any real reason people don't use the @ error control operator (Sparingly, of course) for stuff like this?
#44
Posted 26 June 2011 - 04:00 PM
ini_set( "display_errors", 0);
or by manually updating your php.ini file (which probably isn't possible on a shared host, though perhaps the host could handle that for you. As a general rule, I've heard that it's best to disable display_errors on a live site, so you don't accidentally give away database/server details that might expose security holes. You can learn a bit more about this here: http://php.net/manua...r-reporting.php
In regards to using "@" for error suppression, my impression is that is generally frowned upon, and it should be used as minimally as possible. To summarize others, it seems like the main reasons are:
-- the "@" causes performance issues
-- Parts of the application may fail silently, and using the "@" means it is harder to tell what is going on
-- If you have users that are using the application and something goes wrong, giving them a "white screen of death" when something fails silently is infinitely more frustrating than at least providing an error message they can pass on to support.
For more info, see:
http://php.net/manua...rrorcontrol.php
http://stackoverflow...operator-in-php
Searching for "php error suppression" will probably give you some other results as well. I'm not saying errors shouldn't be suppressed -- they should, to avoid security issues -- but you then need to provide an interface that gives the user an idea of what is failing and what is going on without showing database/server details.
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#45
Posted 27 June 2011 - 05:32 PM
Ben, on 26 June 2011 - 04:00 PM, said:
Yeah, that's what I was saying (or intended to, anyway). Most tutorials don't go over (or ever say) to look into that. Thus, most people who I talked to who learned PHP from them simply think it's normal.
Ben, on 26 June 2011 - 04:00 PM, said:
or by manually updating your php.ini file (which probably isn't possible on a shared host, though perhaps the host could handle that for you. As a general rule, I've heard that it's best to disable display_errors on a live site, so you don't accidentally give away database/server details that might expose security holes. You can learn a bit more about this here: http://php.net/manua...r-reporting.php
For this reason, I generally handle my own errors.
Ben, on 26 June 2011 - 04:00 PM, said:
I would agree. I was mostly wondering what the reasoning behind not using it here was. (Thanks for answering, btw).
Ben, on 26 June 2011 - 04:00 PM, said:
-- the "@" causes performance issues
That is one reason. It's like I said in my post, disables error reporting - runs the code - sets error reporting to what it was:
error_reporting(E_NONE); mysql_connect() ... error_reporting(whatever it was before);
since this essentially turns one statement into 3, I can see how it would effect performance if used widely.
Ben, on 26 June 2011 - 04:00 PM, said:
True. With display_errors you can enable/disable them globally as you please, depending on if you're in the development environment or live.
Ben, on 26 June 2011 - 04:00 PM, said:
Never would actually plan on doing that (Though, that is why in my example I said 'Could not connect to DB')
Generally, I have a nice error page setup for displaying them so that it's build into the template.
Ben, on 26 June 2011 - 04:00 PM, said:
http://php.net/manua...rrorcontrol.php
http://stackoverflow...operator-in-php
Searching for "php error suppression" will probably give you some other results as well. I'm not saying errors shouldn't be suppressed -- they should, to avoid security issues -- but you then need to provide an interface that gives the user an idea of what is failing and what is going on without showing database/server details.
Yeah, leaving the errors displaying enabled by default is bad for a non-development environment. Though I always recommend logging errors (displayed or not) with some environment conditions (variables, etc, if available), and providing a way for users to report them. This way you can go back and see what happened, when it happened, and some environment stuff durring the time.
Anyway, thanks again for your reply
#46
Posted 28 June 2011 - 11:08 PM
Thanks for the very nice tutorial! But I guess I need your idea to enlighten me as I'm still new in PHP
Here is my what I did so far
- my form(name, email, address, picture)
- before send to database, user can preview the form and edit it.
- I'm using($_POST instead of session) on the preview page and "onClick='history.go(-1)'" to edit the page.
- User able to see(name, email, address) and edit at textfield but for image, it missing( I dont know how to call the image that already upload and show it at Edit form). I want to let use delete or reattach new image on it.
Sorry for my english and I hope you understand my problem.
Thanks
#47
Posted 28 June 2011 - 11:49 PM
http://net.tutsplus....f-a-photo-site/
I imagine you can find other tutorials by searching for "upload display image php."
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#48
Posted 29 June 2011 - 12:57 AM
Apologize me for the confusing. let me rephrase and hope you get my question
I have total of 3 page let say register.php, preview.php and submit.php
1. First, user fill at register form(name,email,address, profile picture) and click submit
2. It then go to preview.php page will the information name, email, address and picture --> all the information is not yet store at database.
3. Let say user want to edit it address hence it click button EDIT and it redirect to page register.php with all previous data at text field.
4. Here is the problem. all data for name, email, address are there at text field but not picture.
Question
1. How to call the picture and display it at register.php once user click edit at preview page?
2. I would like also to allow user to delete the picture at edit(register.php).
Hope that make sense. Thanks
#49
Posted 29 June 2011 - 09:19 AM
In regards to the image, once the image has been uploaded on the first page, I would store the path to the uploaded image file in a session variable. Then you can display the image easily:
<img src="<?php echo $your_image_path; ?>" alt="" />
If you need to delete the image, you can have a link that when clicked, checks the session variable for the image path and deletes the specified file. You could even potentially use jquery/AJAX, so that the user never has to leave the page they are currently on. Javascript would be used to call the PHP file that deletes the image, and then based on the result from that PHP file (if it was able to successfully delete the image) you can use Javascript to remove the image from the page and alert the user that the file has been updated.
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#50
Posted 24 July 2011 - 09:53 AM
congratulation for the tutorial but I have a problem with the edit,insert page because when i push the submit button the browser doesn't redirect me to the page that I want but it gives to me: Error loading page and there aren't any new record or edit record.
Thanks and sorry if english isn't perfect
#51
Posted 24 July 2011 - 10:23 AM
toin, on 24 July 2011 - 07:53 AM, said:
congratulation for the tutorial but I have a problem with the edit,insert page because when i push the submit button the browser doesn't redirect me to the page that I want but it gives to me: Error loading page and there aren't any new record or edit record.
Thanks and sorry if english isn't perfect
Make sure the path to the file is correct. You may have a misspelling in the file name.
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#53
Posted 24 July 2011 - 10:35 AM
toin, on 24 July 2011 - 08:34 AM, said:
Can you start a new topic here in the PHP forum, and include the code you are using? Hopefully this is something I can help you figure out.
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#55
Posted 26 July 2011 - 04:36 PM
#56
Posted 26 July 2011 - 04:55 PM
daba daba, on 26 July 2011 - 02:36 PM, said:
The first place I would check would be the line that creates that link within view.php. Make sure it includes "?id=":
echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
If you've double checked that and are still having problems, start a new topic in the PHP forum, post your code and I'll take a look.
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#57
Posted 26 July 2011 - 05:20 PM
Ben, on 26 July 2011 - 04:55 PM, said:
echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
If you've double checked that and are still having problems, start a new topic in the PHP forum, post your code and I'll take a look.
Thanks for the sample link. That worked! I was a little off on the formatting.
#58 Guest_xecvantec
Posted 27 September 2011 - 03:19 AM
Can you upgrade this to do sort by name, and database not use MySQL, use array to store!
#59
Posted 28 September 2011 - 10:31 AM
Using a flat array rather than database is a bit outside the scope of the tutorial, since the entire point of this tutorial was how to work with a database. I'm not sure if an array makes sense, since you'll have to recreate the array every time the page loads -- it won't stay consistent across different pages. You can add/remove/edit items from an array, but those changes won't stay when you visit a different page, or even refresh the page.
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter
#60
Posted 04 October 2011 - 01:12 AM
i used the script you provided for editing.. first of all thanks for the lovely script..
i created a table with id,firstname and lastname fields..as u told and it went perfect with no error... but i tried it for another table and fields 'signum' in place of 'id'.. and it gave me error because signum is varchar type... and id is int... if i keep signum as int then it works fine but gives error for varchar.... there's a line in edit.php which checks that input is_numeric... what to write inplace of that... please provide me with that script...i urgently need it.... THANKS IN ADVANCE...

Help












