Jump to content

php mysql insert problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
a-z-z

a-z-z

    Newbie

  • Members
  • PipPip
  • 11 posts
Hi all,

trying to set a page to insert into a database.

I think the problem may be that I have included both the form, and the should-be-displayed data on the same page, but I wanted to clarify before I try and change it. (the page I am quoting is insert2.php)

Following, is the problem line, when I comment this line out, the page loads fine (doesnt do anything but loads ok), but if I uncomment it, the page loads as absolutely blank:

[FONT=monospace][COLOR=#990000]mysql_query[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"insert into test1 values('[COLOR=#006699][B]$1[/B][/COLOR]', '[COLOR=#006699][B]$2[/B][/COLOR]', '[COLOR=#006699][B]$3[/B][/COLOR]', '[COLOR=#006699][B]$4[/B][/COLOR]', '[COLOR=#006699][B]$5[/B][/COLOR]', '[COLOR=#006699][B]$6[/B][/COLOR]', '[COLOR=#006699][B]$7[/B][/COLOR]', '[COLOR=#006699][B]$8[/B][/COLOR]', '[COLOR=#006699][B]$9[/B][/COLOR]', '[COLOR=#006699][B]$10[/B][/COLOR]', '[COLOR=#006699][B]$11[/B][/COLOR]', '[COLOR=#006699][B]$12[/B][/COLOR]', '[COLOR=#006699][B]$13[/B][/COLOR]', '[COLOR=#006699][B]$14[/B][/COLOR]')"[/COLOR][COLOR=#009900])[/COLOR][/FONT]

Would really appreciate any help, as can't seem to get round this.

Cheers.

Hey you, add something to my base of data, I DARE you!!! <br><br>


<form name="form" action="insert2.php" method="post">

<table>

<tr><td>First Name:</td><td><input type="text" name="1" /></td></tr>
<tr><td>Surname:</td><td><input type="text" name="2" /></td></tr>
<tr><td>Address Line 1:</td><td><input type="text" name="3" /></td></tr>
<tr><td>City:</td><td><input type="text" name="4" /></td></tr>
<tr><td>County:</td><td><input type="text" name="5" /></td></tr>
<tr><td>Postcode:</td><td><input type="text" name="6" /></td></tr>
<tr><td>Telephone - Home:</td><td><input type="text" name="7" /></td></tr>
<tr><td>Telephone - Mobile:</td><td><input type="text" name="8" /></td></tr>
<tr><td>Email:</td><td><input type="text" name="9" /></td></tr>
<tr><td>Bike Manufacturer:</td><td><input type="text" name="10" /></td></tr>
<tr><td>Bike Model:</td><td><input type="text" name="11" /></td></tr>
<tr><td>Bike Colour:</td><td><input type="text" name="12" /></td></tr>
<tr><td>Bike Serial Number:</td><td><input type="text" name="13" /></td></tr>
<tr><td>ID:</td><td><input type="text" name="14" /></td></tr>

</table>

<input type="submit" name="Submit" value="Submit" />

</form>



[COLOR=#000000][B]<?php[/B][/COLOR]

$1 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'1'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$2 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'2'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$3 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'3'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$4 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'4'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$5 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'5'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$6 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'6'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$7 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'7'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$8 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'8'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$9 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'9'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$10 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'10'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$11 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'11'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$12 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'12'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$13 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'13'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]
$14 [COLOR=#339933]=[/COLOR] [COLOR=#339933]@[/COLOR][COLOR=#000088]$_POST[/COLOR][COLOR=#009900][[/COLOR][COLOR=#0000ff]'14'[/COLOR][COLOR=#009900]][/COLOR][COLOR=#339933];[/COLOR]


[COLOR=#000088]$db_host[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#0000ff]'notactualhost'[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#000088]$db_user[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#0000ff]'notactualuser'[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#000088]$db_pwd[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#0000ff]'notactualpw'[/COLOR][COLOR=#339933];[/COLOR]

[COLOR=#000088]$database[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#0000ff]'notactualdb'[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#000088]$table[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#0000ff]'test1'[/COLOR][COLOR=#339933];[/COLOR]

[COLOR=#b1b100]if[/COLOR] [COLOR=#009900]([/COLOR][COLOR=#339933]![/COLOR][COLOR=#990000]mysql_connect[/COLOR][COLOR=#009900]([/COLOR][COLOR=#000088]$db_host[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#000088]$db_user[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#000088]$db_pwd[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR]
    [COLOR=#990000]die[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"Can't connect to database"[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]

[COLOR=#b1b100]if[/COLOR] [COLOR=#009900]([/COLOR][COLOR=#339933]![/COLOR][COLOR=#990000]mysql_select_db[/COLOR][COLOR=#009900]([/COLOR][COLOR=#000088]$database[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR]
    [COLOR=#990000]die[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"Can't select database"[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]

[COLOR=#990000]mysql_query[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]"insert into test1 values('[COLOR=#006699][B]$1[/B][/COLOR]', '[COLOR=#006699][B]$2[/B][/COLOR]', '[COLOR=#006699][B]$3[/B][/COLOR]', '[COLOR=#006699][B]$4[/B][/COLOR]', '[COLOR=#006699][B]$5[/B][/COLOR]', '[COLOR=#006699][B]$6[/B][/COLOR]', '[COLOR=#006699][B]$7[/B][/COLOR]', '[COLOR=#006699][B]$8[/B][/COLOR]', '[COLOR=#006699][B]$9[/B][/COLOR]', '[COLOR=#006699][B]$10[/B][/COLOR]', '[COLOR=#006699][B]$11[/B][/COLOR]', '[COLOR=#006699][B]$12[/B][/COLOR]', '[COLOR=#006699][B]$13[/B][/COLOR]', '[COLOR=#006699][B]$14[/B][/COLOR]')"[/COLOR][COLOR=#009900])[/COLOR] or [COLOR=#990000]die[/COLOR][COLOR=#009900]([/COLOR][COLOR=#990000]mysql_error[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#b1b100]echo[/COLOR] [COLOR=#0000ff]"inserted into db"[/COLOR][COLOR=#339933];[/COLOR]

[COLOR=#000000][B]?>[/B][/COLOR]


#2
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Dunno what's wrong with your code, but:
1. Put these lines at top of your script:

error_reporting(E_ALL);

ini_set('display_errors', true);

2. Remove @ from your code. Don't use @ at all. Use PHP: isset - Manual and this:

if ($_SERVER['REQUEST_METHOD'] === 'POST') { // form was posted

    $name = $_POST['name']; // you can safely access to posted data

}

3. Instead of $1 use $firstName, instead of $2 use $surname and so on.
4. You have to escape data before passing it to database, see docs for PHP: mysql_real_escape_string - Manual and PHP: Security - Manual

Rewrite your script with this changes and you will find such simple errors yourself.

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
It appears he is attempting to access variables through registered globals, this is very bad. You should have name="surname" and other useful names, and access them through $_POST['surname'] not by numbers, be sure to sanitize each variable (check the FAQs on this board section)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
a-z-z

a-z-z

    Newbie

  • Members
  • PipPip
  • 11 posts
I have a lot to learn!

Vladimir said:

3. Instead of $1 use $firstName, instead of $2 use $surname and so on.

I've changed the names from 1, 2, 3, 4, etc.. to a, b, c, d, etc.. and it seems to be working fine.

I appreciate the help and clarity. Could you explain why it didn't work with numbers?

Cheers both of you.

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
To read from the PHP documentation regarding variables:

Quote

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
Source: PHP: Basics - Manual

It says that variables must start with a-z, A-Z or underscore,
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall