472,983 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

HELP: "insert into" problem in PHP

Hi, I need urgent help with a novice problem. I would appreciate any
advice, suggestions... Thanks a lot in advance! Here it is:

I created a sign-up sheet (reg.html) where people fill in their first
name, last name, email, etc. The data are then sent to a PHP script
(reg.php). The data are then inserted into a table (reg) in MS SQL
server. I have declared the variables like this:

if (!(isset($_POST['FirstName']))) {
$FirstName = "" ;
} else {
$FirstName = $_POST['FirstName'] ;
}

Then I said the following to make sure people fill in all the info:

if(empty($FirstName) OR empty($LastName) OR empty($Email))
{
echo "Oops, you must complete the form to register. Please use the
browser back button to go back and complete the form.";
echo "</body></html>";
exit;
}

The statement that insert the data to the table is like this:

$qry = "insert into reg values ('$FirstName','$LastName','$Email')";

However, this doesn't work. I looked into examples online, but none of
the "insert into" statements handle varibles (they all insert actual
values such as "Hanna, Smith, hs@yahoo.com', etc.)

Could you tell me what went wrong with this script? I am pretty sure
the problem originates from the "insert" statement.

Thanks!

A PHP Newbie
Jul 16 '05 #1
2 5568
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whilst lounging around on 5 Aug 2003 12:50:24 -0700,
kw*****@yahoo.com (newbie_mw) amazingly managed to produce the
following with their Etch-A-Sketch:
Hi, I need urgent help with a novice problem. I would appreciate
any advice, suggestions... Thanks a lot in advance! Here it is:

I created a sign-up sheet (reg.html) where people fill in their
first name, last name, email, etc. The data are then sent to a PHP
script (reg.php). The data are then inserted into a table (reg) in
MS SQL server. I have declared the variables like this:

if (!(isset($_POST['FirstName']))) {
$FirstName = "" ;
} else {
$FirstName = $_POST['FirstName'] ;
}

Then I said the following to make sure people fill in all the info:

if(empty($FirstName) OR empty($LastName) OR empty($Email))
{
echo "Oops, you must complete the form to register. Please use the
browser back button to go back and complete the form.";
echo "</body></html>";
exit;
}

The statement that insert the data to the table is like this:

$qry = "insert into reg values
('$FirstName','$LastName','$Email')";

However, this doesn't work. I looked into examples online, but none
of the "insert into" statements handle varibles (they all insert
actual values such as "Hanna, Smith, hs@yahoo.com', etc.)

Could you tell me what went wrong with this script? I am pretty
sure the problem originates from the "insert" statement.

Thanks!

A PHP Newbie

And the error you received is?

I have an _idea_ what the problem _might_ be, but how can I tell for
sure without knowing what error I'm thinking about assisting you
with?

My initial thought / theory is that you've got more columns than just
those 3 (possibly an ID column amongst other things). Try something
like:
$sql = "
INSERT INTO reg
(
firstname,
lastname,
email
)
VALUES (
$FirstName,
$LastName,
$Email
)
";
I've obviously assumed that you have 'firstname', 'lastname' and
'email' as table columns, you may need to ammend to suit your exact
table structure.
HTH.

Regards,

Ian

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBPzAMGWfqtj251CDhEQIkbgCgo0niSW+WsOlaeRxB/IHiDtFUDtoAoJXp
BhNxNkv9wZcsnp/ce5ukS3ZI
=0C9T
-----END PGP SIGNATURE-----

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.
Jul 16 '05 #2
Hi Ian,

Thanks for help! The columns and variables are exactly matched so it shouldn't
be the problem. Actually the original html and php files are pretty long. But
I guess a more detailed script should help you diagnose the problem. The error
message to the below script is:
---------------------------------
call someoneDB Error:
Warning: mssql_num_rows(): supplied argument is not a valid Sybase result
resource ... line 88
----------------------------------

//... is the folder this file is in//

Here is the (almost) full script:

<?php

// some MS SQL server connection thing that I can't show here :-) //
// below is the original script.//

if (!(isset($_POST['NetID']))) {
$NetID = "" ;
} else {
$NetID = $_POST['NetID'] ;
}

if (!(isset($_POST['FirstName']))) {
$FirstName = "" ;
} else {
$FirstName = $_POST['FirstName'] ;
}

if (!(isset($_POST['LastName']))) {
$LastName = "" ;
} else {
$LastName = $_POST['LastName'] ;
}

if (!(isset($_POST['Email']))) {
$Email = "" ;
} else {
$Email = $_POST['Email'] ;
}

if (!(isset($_POST['Major']))) {
$Major = "" ;
} else {
$Major = $_POST['Major'] ;
}

if (!(isset($_POST['Classification']))) {
$Classification = "" ;
} else {
$Classification = $_POST['Classification'] ;
}

if (!(isset($_POST['How']))) {
$How = "" ;
} else {
$How = $_POST['How'] ;
}

if (!(isset($_POST['Description']))) {
$Description = "" ;
} else {
$Description = $_POST['Description'] ;
}

if(empty($NetID) OR empty($FirstName) OR empty($LastName) OR empty($Email) OR
empty($Major) OR empty($Classification) OR empty($How) OR empty($Description))

{
echo "Oops, you must complete the form to register. Please use the browser
back button to go back and complete the form.";
echo "</body></html>";
exit;
}

$qry = "insert into reg values (";
$qry = $qry."'$NetID'";
$qry = $qry.",'$FirstName'";
$qry = $qry.",'$LastName'";
$qry = $qry.",'$Email'";
$qry = $qry.",'$Major'";
$qry = $qry.",'$Classification'";
$qry = $qry.",'$How'";
$qry = $qry.",'$Description'";
$qry = $qry.")";

$rs=$db->query ($qry);
if (DB::iserror($rs)){print"call someone"; print $rs->getMessage();}

$result = mssql_query("SELECT * FROM reg");
$num_rows = mssql_num_rows($result) ;

if ($num_rows > 10)
{
echo "Sorry, our registration is full. Please stay tuned till the next
one.";
}

else
{
echo "Congratulations, $FirstName! Your have registered for --- ";
echo "<p>";
}

?>

Hope it is clearer now. What's most frustrating is that it worked for a while,
but then when I expanded the columns from 3 to 8 and it just messed up. So
what went wrong?

Newbie

"Ian.H [dS]" wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Whilst lounging around on 5 Aug 2003 12:50:24 -0700,
kw*****@yahoo.com (newbie_mw) amazingly managed to produce the
following with their Etch-A-Sketch:
Hi, I need urgent help with a novice problem. I would appreciate
any advice, suggestions... Thanks a lot in advance! Here it is:

I created a sign-up sheet (reg.html) where people fill in their
first name, last name, email, etc. The data are then sent to a PHP
script (reg.php). The data are then inserted into a table (reg) in
MS SQL server. I have declared the variables like this:

if (!(isset($_POST['FirstName']))) {
$FirstName = "" ;
} else {
$FirstName = $_POST['FirstName'] ;
}

Then I said the following to make sure people fill in all the info:

if(empty($FirstName) OR empty($LastName) OR empty($Email))
{
echo "Oops, you must complete the form to register. Please use the
browser back button to go back and complete the form.";
echo "</body></html>";
exit;
}

The statement that insert the data to the table is like this:

$qry = "insert into reg values
('$FirstName','$LastName','$Email')";

However, this doesn't work. I looked into examples online, but none
of the "insert into" statements handle varibles (they all insert
actual values such as "Hanna, Smith, hs@yahoo.com', etc.)

Could you tell me what went wrong with this script? I am pretty
sure the problem originates from the "insert" statement.

Thanks!

A PHP Newbie


And the error you received is?

I have an _idea_ what the problem _might_ be, but how can I tell for
sure without knowing what error I'm thinking about assisting you
with?

My initial thought / theory is that you've got more columns than just
those 3 (possibly an ID column amongst other things). Try something
like:

$sql = "
INSERT INTO reg
(
firstname,
lastname,
email
)
VALUES (
$FirstName,
$LastName,
$Email
)
";

I've obviously assumed that you have 'firstname', 'lastname' and
'email' as table columns, you may need to ammend to suit your exact
table structure.

HTH.

Regards,

Ian

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBPzAMGWfqtj251CDhEQIkbgCgo0niSW+WsOlaeRxB/IHiDtFUDtoAoJXp
BhNxNkv9wZcsnp/ce5ukS3ZI
=0C9T
-----END PGP SIGNATURE-----

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.


Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: newbie_mw | last post by:
Seems my post was buried in more cries for help :-) I will try again. It's probably a very novice question so please take a look! Thanks!...
9
by: Luc Dal | last post by:
Hello, I've serious problem using ASP under WindowsXP sp2. I get the following reply (sorry it's in french) Erreur de compilation Microsoft VBScript error '800a0401' Fin d'instruction...
5
by: Chad Richardson | last post by:
Is there a way in SQL Server 2000 to extract data from a table, such that the result is a text file in the format of "Insert Into..." statements, i.e. if the table has 5 rows, the result would be 5...
2
by: CFW | last post by:
I use the following flawlessly to insert a single field: strSQL = "Insert into (Casket) Values " _ & "(" & conQuote & NewCasket & conQuote & ")" Set db = CurrentDb If MsgBox(NewCasket & " is...
3
by: Ed | last post by:
Hi, I want to load data to a table in Sql Server from a dataset table in my vb.net app using a dataAdapter. I know how to do this as follows (my question is to see if I can reduce the amount...
0
by: MarceloLinero | last post by:
Hi, i`m problem whith it !! help me ! No found way for insert in table directy in database sql express and no undertand why ! steps in general: 1. create coneccion ready 2. declarate a...
4
by: Takeadoe | last post by:
Dear NG - I've got a bunch of SQL statements (Insert Into "Access Table Name") generated from another software program. They look like this: Pasting these lines into the SQL view as they are...
13
by: shankindc | last post by:
Hi, I have a data entry form which opens default values each time the form is open. Requirement is that users can edit existing data in the form. When the form is closed, it shouldnt update the...
6
by: ewpatton | last post by:
Good day, I've been trying to work with SQL and an Access database in order to handle custom user profiles. I haven't had any trouble reading from my database, but inserting new entries into...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.