473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Script Help required

keyvanrahmadi
Hiya guys,

I have created a simple form which the script is as follow:

[HTML]
<form name="Untitled-11A" method="post">

MODEL TYPE:<select name=“model_id”>
<option value=“model1”/>Clio
<option value=“model2”/>Espace
<option value=“model3”/>TT
<option value=“model4”/>306
<option value=“model5”/>Vectra
</select>

Choose Colour:<select name=“colour_id”>
<option value=“colour1”/>Blue
<option value=“colour2”/>Green
<option value=“colour3”/>Red
<option value=“colour4”/>White
<option value=“colour5”/>Black
<option value=“colour6”/>Yellow
<option value=“colour7”/>Silver
</select>

Transmission TYPE:<select name=“transmission_id”>
<option value=“trans1”/>Automatic
<option value=“trans2”/>Manual
</select>

Location:<select name=“location_id”>
<option value=“location1”/>London
<option value=“location2”/>Liverpool
</select>

Car Reg:<input type="text" name="reg" size="20">

<input type="submit" value="Submit" name="submit">
</form>
</select>
[/HTML]

Then i have the following script which is suppoe to insert the chosen values into a mysql databse, but as you most probebly have guessed by now, i am not sure why it dosent. can you guys look over the code and enlighten me where my mistake is please.

[PHP]

<?php

if (isset($_POST['reg']))
$reg = $_POST['reg'];

if (isset($_POST['model_id']))
$mode_id = $_POST['model_id'];

if (isset($_POST['colour_id']))
$colour_id = $_POST['colour_id'];

if (isset($_POST['transmisson_id']))
$transmisson_id = $_POST['transmisson_id'];

if (isset($_POST['location_id']))
$location_id = $_POST['location_id'];

//make a connection to database

$DB = mysql_connect("host", "usr", "pass") or die("omg it didnt work!");
mysql_select_db("db", $DB) or die("aaah! so close!");

$insert = "INSERT INTO car_o2 (reg, model_id, colour_id, transmission_id, location_id ) VALUES ('$reg', '$model_id', '$colour_id', '$transmission_id', $location_id)";

if (mysql_query($insert, $DB) === FALSE)
{
echo 'Car was not inserted into database.<br />';
}
else
{
echo 'Car was successfully inserted into database.<br />';
echo 'CLICK HERE to return to admin page';
}
?>

[/PHP]

The mysql table has the follwoing colums: reg, model_id, colous_id, transmission_id and location_id.

Any help would be appriciated.

keyvan
Mar 2 '07 #1
15 1614
vssp
268 100+
<?php $result = mysql_query($insert)
or die("Invalid query: " . mysql_error());
?>
Mar 2 '07 #2
ronverdonk
4,258 Expert 4TB
What vssp means is that you should echo the MySQL error that the insert statement returns. As follows:

[php] if (!mysql_query($insert, $DB))
{
die ('Car was not inserted into database, error is: '.mysql_error());
}
[/php]

Ronald :cool:
Mar 2 '07 #3
What vssp means is that you should echo the MySQL error that the insert statement returns. As follows:

[php] if (!mysql_query($insert, $DB))
{
die ('Car was not inserted into database, error is: '.mysql_error());
}
[/php]

Ronald :cool:
Ronald can you please explain the difference to me, i am new to php and not sure i understand it very well. I like to apologise if i sound too stupid.

keyvan
Mar 2 '07 #4
ronverdonk
4,258 Expert 4TB
The following statement adds the MySQL error message mysql_error() to your own error message:

Expand|Select|Wrap|Line Numbers
  1. die ('Car was not inserted into database, error is: '.mysql_error());
So when the MySQL statement returns an error you can see what went wrong and where.

Ronald :cool:
Mar 2 '07 #5
It returns the following error massage:

Car was not inserted into database, error is: 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 ')' at line 1.

I have checked all the script and i cant find the problem?
Mar 2 '07 #6
ronverdonk
4,258 Expert 4TB
Are you sure that the $location_id variable holds numeric data? I mean: could it be a character field that includes funny characters, like a blank. Because then the last field in this insert statement would be erroneous.
Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO car_o2 
  2. (reg, model_id, colour_id, transmission_id, location_id ) VALUES 
  3. ('$reg', '$model_id', '$colour_id', '$transmission_id', $location_id)";
I found another error:
Expand|Select|Wrap|Line Numbers
  1.     $mode_id = $_POST['model_id'];
shoulbe
Expand|Select|Wrap|Line Numbers
  1.     $model_id = $_POST['model_id'];
Ronald :cool:
Mar 2 '07 #7
Are you sure that the $location_id variable holds numeric data? I mean: could it be a character field that includes funny characters, like a blank. Because then the last field in this insert statement would be erroneous.
Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO car_o2 
  2. (reg, model_id, colour_id, transmission_id, location_id ) VALUES 
  3. ('$reg', '$model_id', '$colour_id', '$transmission_id', $location_id)";
I found another error:
Expand|Select|Wrap|Line Numbers
  1.     $mode_id = $_POST['model_id'];
shoulbe
Expand|Select|Wrap|Line Numbers
  1.     $model_id = $_POST['model_id'];
Ronald :cool:
Regarding $model typo, not sure how i managed that, but its corrected now and i am still getting the same error massage. $location_id is a mediumint, auto increment and i have changed the insert value into numeric and still getting the same error.

Now i really dont have a clue whats the problem.

keyvan :(
Mar 2 '07 #8
ronverdonk
4,258 Expert 4TB
Then only 'brute force' can be used to determine the error.

You must now also display the select statement itself in the error message. That way you can see where the MySQL error is located (hope).

So replace the query by the following statement and see what the error message including the constructed sql statement is.

[php]if (!mysql_query($insert, $DB)) {
die ("Car was not inserted into database.<br>SQL statement is: $insert<br>Error is: ".mysql_error());
}
[/php]

Ronald :cool:
Mar 3 '07 #9
Then only 'brute force' can be used to determine the error.

You must now also display the select statement itself in the error message. That way you can see where the MySQL error is located (hope).

So replace the query by the following statement and see what the error message including the constructed sql statement is.

[php]if (!mysql_query($insert, $DB)) {
die ("Car was not inserted into database.<br>SQL statement is: $insert<br>Error is: ".mysql_error());
}
[/php]

Ronald :cool:
here is the error massage:

Car was not inserted into database.
SQL statement is: INSERT INTO car_o2 (reg, model_id, colour_id, transmission_id, location_id ) VALUES ('', '', '', '', )
Error is: 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 ')' at line 1
Mar 3 '07 #10
ronverdonk
4,258 Expert 4TB
So none of your variables contain any data.

But hold on: looking at your html form (I really haven't looked at that before, just at the POST processing script) I see that you did not specify the <option> correcly. An option statement looks like:

[html]<option value="value">text</option>[/html]

Ronald :cool:
Mar 3 '07 #11
So none of your variables contain any data.

But hold on: looking at your html form (I really haven't looked at that before, just at the POST processing script) I see that you did not specify the <option> correcly. An option statement looks like:

[html]<option value="value">text</option>[/html]

Ronald :cool:
Car was not inserted into database.
SQL statement is: INSERT INTO car_o2 (reg, model_id, colour_id, transmission_id, location_id ) VALUES ('P000 KEY', '', '', '', )
Error is: 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 ')' at line 1.

I also noticed i have started my form name="form" rather than Form Action="form", if that makes any difference.
Mar 3 '07 #12
its very strange i changed the form from drop down menu to text and it works fine..although it woudl have been far better as a drop down menu. I am still not sure what the problem is.

keyvan
Mar 3 '07 #13
ronverdonk
4,258 Expert 4TB
You still have problems. As I can see the $location_id cannot be an integer, it is a character field, how could you otherwise store 'London' or 'Liverpool' in it?

Since this is dragging into an endless discussion, I have re-arranged the code you showed an put it into one script. That makes it easier to maintain and see what is ahppening. And it works, so try it and adapt it to your own needs. But do not forget to make your location_id field in the database a VARCHAR data type!

[php]<?php
// -------------------------------------------
// if form is submitted, process POSTed data
// -------------------------------------------
if (isset($_POST['submit'])) {

if (isset($_POST['reg']))
$reg = $_POST['reg'];
if (isset($_POST['model_id']))
$model_id = $_POST['model_id'];
if (isset($_POST['colour_id']))
$colour_id = $_POST['colour_id'];
if (isset($_POST['transmisson_id']))
$transmisson_id = $_POST['transmisson_id'];
if (isset($_POST['location_id']))
$location_id = $_POST['location_id'];

//make a connection to database
$DB = mysql_connect("localhost", "xxxxx", "yyyyy")
or die("Connect to server failed: ".mysql_error());
mysql_select_db("zzzz", $DB)
or die("DB select failed: ".mysql_error());

$insert = "INSERT INTO car_o2 (reg, model_id, colour_id, transmission_id, location_id ) VALUES ('$reg', '$model_id', '$colour_id', '$transmission_id', '$location_id')";

if (mysql_query($insert, $DB) === FALSE) {
die ("Car was not inserted into database.<br>SQL statement is: $insert<br>Error is: ".mysql_error());
}
else {
echo 'Car was successfully inserted into database.<br />';
echo 'CLICK HERE to return to admin page';
exit;
}
} // end isset

// ------------------------------------------------
// form is not submitted, show the data entry form
// ------------------------------------------------
?>

<form name="Untitled-11A" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

MODEL TYPE:<select name="model_id”>
<option value="model1">Clio</option>
<option value="model2">Espace</option>
<option value="model3">TT</option>
<option value="model4">306</option>
<option value="model5">Vectra</option>
</select>
<br />
Choose Colour:<select name="colour_id">
<option value="colour1">Blue</option>
<option value="colour2">Green</option>
<option value="colour3">Red</option>
<option value="colour4">White</option>
<option value="colour5">Black</option>
<option value="colour6">Yellow</option>
<option value="colour7">Silver</option>
</select>
<br />
Transmission TYPE:<select name="transmission_id">
<option value="trans1">Automatic</option>
<option value="trans2">Manual</option>
</select>
<br />
Location:<select name="location_id">
<option value="location1">London</option>
<option value="location2">Liverpool</option>
</select>
<br />
Car Reg:<input type="text" name="reg" size="20">
<br />
<input type="submit" value="Submit" name="submit">
</form>
</select>[/php]

Ronald :cool:
Mar 3 '07 #14
Thank you, as usuall you have been more than helpfull.

keyvan
Mar 3 '07 #15
ronverdonk
4,258 Expert 4TB
You are welcome. I hope the code works for you. Please let me know if it isn't.

Ronald :cool:
Mar 3 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Web Master | last post by:
Hi, I am having a little issue with Jacks Form mail php script. I have installed it and configured the form to get it to work, but for some bizarre reason I have 2 issues I can't seem to debug....
2
by: livin | last post by:
I'm hoping someone knows of an example script I can see to help me build mine. I'm looking for an easy way to automate the below web site browsing and pull the data I'm searching for. Here's...
8
by: Sergei | last post by:
Hi, I am displaying modal dialog and passing values from the main form to the modal dialog and back. It works fine but if I used the following syntax on Page_Load(just for testing) in VB to...
9
by: shara | last post by:
Hello there, I've a script that uses sql,php and html.I need this script to call another script.The script generates check boxes for the users to select, before the button is clicked.But after the...
3
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of...
2
Mr aldo
by: Mr aldo | last post by:
Hello I have this Sendmail Script here that uses Javascript, so when a field is left empty it makes a pop up saying a field was left empty and you click OK but when you click OK it proccesses it and...
3
by: traceable1 | last post by:
Is there a way I can set up a SQL script to run when the instance starts up? SQL Server 2005 SP2 thanks!
2
pradeepjain
by: pradeepjain | last post by:
<script src="getimages.php"></script> i am using this to run a script but dring validation it gives error as " Line 285, Column 51: required attribute "type" not specified. <div...
8
by: howa | last post by:
Just found a funny things.. in HTML4's dtd, http://www.w3.org/TR/html4/strict.dtd language is not valid, i.e. <!ELEMENT SCRIPT - - %Script; -- script statements --> <!ATTLIST...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.