Why MySQL (using PHP's "mysql_query()" function) INSERT INTO statements messed up?...

  • Thread starter Thread starter P.S.P.
  • Start date Start date
P

P.S.P.

Guest
...[UPDATED question]? Oops, I've accidentally clicked put my original question to vote, here's the better version:

I just started MySQL, and I'm currently in the basics, the real basics, where I only know how to connect to my own localhost database, create a database, create tables, and insert values manually to database tables. However, when I tried doing some HTML forms using PHP scripts to add new rows on a database table, the information from the form gets inserted more than once, sometimes even 5 times, exactly, and only in one submission... Does anyone know SQL here? Using the SQL's "INSERT INTO" statements manually, as in directly in the PHP script, without using HTML forms to collect manually-submitted user info, I was able to create only one database entry to a table... I've set a primary key, but it's only for the ID of one table, and if I set more than one primary key PHP creates an error saying multiple primary keys are not allowed... Anyway let me sum it up for those who understands PHP and MySQL very well:

I need help making a script that will use SQL's "INSERT INTO (...) VALUES (...) " statements, which the value is collected from a HTML form and submitted once by a user... and only ends up inserting 1 entry in a database table, and not inserting 2-6 entries at once... Thanks!

By the way, I created this form for posting guestbook entries to my database...

NOTE: Yahoo might do some strange stuff like replacing some codes with ellipsis(...). I'm 100% sure there's no error in my codes that's caused by incomplete codes... so... Please tell me what I did wrong, and it's most likely not from incomplete scripts, it's Yahoo!... Also, this is all practice I do based on what I learned from W3Schools, so please don't give me the W3School's PHP-MySQL page because I learned it from there! Anyway, thanks in advance for all of your help!..AND REMEMBER! My problem don't lay in I can't insert info to database, it's all OK, but my scripts, which I learned from W3Schools, when used with forms, inserts double or multiple entries in one submission...

Form Page (index.php):

<html
lang="en">

<head>

<title>Second MySQL-Database Practice!</title>

<script
type="text/javascript"
src="/JS/SCRIPTS/For_MySQL_Practice/Second_Practice/form_scripts.js"></script>

</head>

<body>

<h2>

A “Guestbook” – Sign It!</h2>

<p
style="text-align:center;
border:2px dashed #00f;
padding:5px;
width:150px;
background-color:#eee;
overflow:auto;">

<span style="font-weight:bold;color:#f00;">*</span> Marks <span style="font-weight:bold;font-style:italic;">required</span> fields</p>

<form
action="Insert_2_Guestbook.php"
method="post"
onsubmit="return check_form(this,'firstname','comments','e_mail');">

Your Firstname<span style="font-weight:bold;color:#f00;">*</span>: <input type="text" name="firstname" id="firstname" maxlength="20"> (Max: 20 characters)

<br>

Your Lastname: <input type="text" name="lastname" id="lastname" maxlength="35"> (Max: 35 characters)

<br>

Your Age: <input type="text" name="age" id="age" maxlength="3" size="1"> (Has to be a number!)

<br>

Your Gender:

<input
type="radio"
name="gender"
value="male">Male

<input
type="radio"
name="gender"
value="female">Female (Please select one!)

<br>

Your Website's URL: <input type="text" name="website" id="website" maxlength="50"> (Max: 50 characters. <span style="font-style:italic;">If filled</span>, please provide a valid URL.)

<br>

Your E-Mail Address: <input type="text" name="e_mail" id="e_mail" maxlength="50"> (Max: 50 characters. <span style="font-style:italic;">If filled</span>, please provide a valid E-Mail address.)

<br>

Your Comments<span style="font-weight:bold;color:#f00;">*</span>:

<br>

<textarea
onkeyup="count_chars(this);"
onkeydown="count_chars(this);"
name="comments"
id="comments"
cols="50"
rows="10"
style="font-family:'Times New Roman';
font-size:100%;"></textarea> <span id="Comments_RemainChar_Counter">(Max: 450 characters)</span>

<br>

<input
type="reset"
name="reset"
id="reset"
value="Clear/Reset">

<input
type="submit"

name="submit"
id="submit"
value="Sign My Guestbook!"></form>

</body>

</html>

The action and summary page (Insert_2_Guestbook.php):

<html
lang="en">

<head>

<title>COMPLETE Summary of Guestbook Database Processing Results</title>

</head>

<body>

<h1
style="text-align:center;
font-family:Verdana;
font-size:120%;">

COMPLETE Summary of Guestbook Database Processing Results</h1>

<?php

$Connect=mysql_connect("localhost","***","***");

if (!$Connect)
{

die("Error connecting to the database because: ".mysql_error());

}

if (mysql_query("CREATE DATABASE Guest_Book",$Connect))
{

echo "The Guest Book's database has been successfully created for the first time.";

}

else
{

echo "The Guest Book's database has NOT been successfully crea
 
Back
Top