You are here: Re: on input form showing error message for duplicate entry. « All PHP « IT news, forums, messages
Re: on input form showing error message for duplicate entry.

Posted by Breklin on 10/17/06 19:49

Linda,

This is how I would write that:

[php]
<?php

//login details


@mysql_connect($host,$user,$password)
or die("Unable to connect with server!");
@mysql_select_db($database)
or die("Unable to connect database, please try later!");

-- No need for a form set ID value. Use the Auto-Increment feature in MySQL
-- I have never used the mysql_real_escape_string function. A variation is below.

// Set Form Data into PHP Vars and format against injection attack
$name = htmlspecialchars($_POST['name']);
$info = htmlspecialchars($_POST['info']);
$pic = htmlspecialchars($_POST['pic']);
$lg_pic = htmlspecialchars($_POST['lg_pic']);

-- QUESTION: What data time is 'Price': int, varchar, float, etc?

$price = htmlspecialchars($_POST['price']);

-- When using this type of insert, you must make sure you are placing data values in every field of the table. If you are not listing even one field in the VALUES portion of the query, you will get a query error.

$query = "INSERT INTO floral VALUES ('','$name','$info','$pic','$lg_pic','$price')";

mysql_query($query)

-- ADD the following to see what your error is:

or die(mysql_error());

header("Location: added.php");

mysql_close();

?>

That should help to get you started working out the problem. The only
way to really learn PHP is through trial and error.

Breklin wrote:
> #1, if you have set your table to auto_increment for the ID field, you
> do not need to use a post to put that data into that field. This is a
> great way to eliminate the possibility of duplicate ID data.
>
> Remove the $_POST['id'] and in the SQL statement, use '' instead of
> '$id' like this:
>
> $query = "INSERT INTO floral VALUES ('','$name','$info','$pic','$lg_pic','$price')";
>
> There are many PHP form validation scripts out there with messaging. Try going to phpclasses.org for these types of scripts.
>
>
>
> linda wrote:
>
>> I very new to php could someone please explain how to write an error message
>> into my AddNew.php below. id is set to unique so at the moment if someone
>> adds a product and the id is already in the database, it just doesn't enter
>> anything. Ideally I would like to display an error message, with a back
>> button to correct the id.
>>
>> So far I've tried running a SELECT query with an if statement prior to the
>> INSERT but I just don't know enough php to get this to work. I would be
>> very greatful for any info and advice from those in this newsgroup.
>>
>> Many, many thanks in advance,
>> Linda
>>
>>
>> [php]
>> <?php
>>
>> //login details
>>
>>
>> @mysql_connect($host,$user,$password) or die("Unable to connect with
>> server!");
>> @mysql_select_db($database)or die("Unable to connect database, please try
>> later!");
>>
>> $id=mysql_real_escape_string($_POST['id']); //This is set to unique
>> $name=mysql_real_escape_string($_POST['name']);
>> $info=mysql_real_escape_string($_POST['info']);
>> $pic=mysql_real_escape_string($_POST['pic']);
>> $lg_pic=mysql_real_escape_string($_POST['lg_pic']);
>> $price=mysql_real_escape_string($_POST['price']);
>>
>> $query = "INSERT INTO floral VALUES
>> ('$id','$name','$info','$pic','$lg_pic','$price')";
>> mysql_query($query);
>> header("Location: added.php");
>>
>> mysql_close();
>> ?>
>>
>>
>>
>>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация