<HTML>
<?php
if (empty($CSP_Name) || empty($Supervisor) || empty($Rep_Number) || empty($Department) || empty($Account_No) || empty($First_Name) || empty($Last_Name) || empty($Phone) || empty($ShipTo_Address) || empty($ShipTo_City) || empty($ShipTo_State) || empty($ShipTo_ZIP) || empty($Accept))
{echo "<h2>Please make sure you have filled in all required fields. Please use your browsers Back button</h2>\n";
 echo "<h2>******REQUEST WAS NOT SUBMITTED******</h2>\n";}
if (isset ($_POST['chk8'])) {
$req1 = "Pre Band Mailer\n";
} else {
$req1 = "None\n";
}
if (isset ($_POST['chk9'])) {
$req2 = "At Brand Mailer\n";
} else {
$req2 = "None\n";
}
if (isset ($_POST['chk10'])) {
$req3 = "Welcome Kit\n";
} else {
$req3 = "None\n";
}
if (isset ($_POST['chk11'])) {
$req4 = "Three Product Welcome Kit -1\n";
} else {
$req4 = "None\n";
}
if (isset ($_POST['chk12'])) {
$req5 = "Three Product Welcome Kit -2\n";
} else {
$req5 = "None\n";
}
if (isset ($_POST['chk13'])) {
$req6 = "...\n";
} else {
$req6 = "None\n";
}
if (isset ($_POST['chk14'])) {
$req7 = "...\n";
} else {
$req7 = "None\n";
}
$ShipTo_Address = stripcslashes($ShipTo_Address);
$Additional_Info = stripcslashes($Additional_Info);
?>
<?php
$usr = "national_twc";
$pwd = "timewarnercable";
$db = "national_test";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
if ($REQUEST_METHOD=="POST") {
if mysql_query("SELECT * FROM test WHERE Account_No = '$Account_No'"){
echo ("<center><h1>Sorry. Your account is already added.</h1></center>\n");
}    else {
$SQL = " INSERT INTO twc ";
$SQL = $SQL . " (DATE, CSP_Name, Supervisor, Rep_Number, Department, Email, req1, req2, req3, req4, req5, req6, req7, Account_No, First_Name, Last_Name, Phone, ShipTo_Address, ShipTo_City, ShipTo_State, ShipTo_ZIP, Additional_Info, Accept) VALUES ";
$SQL = $SQL . " (now(), '$CSP_Name', '$Supervisor', '$Rep_Number', '$Department', '$Email', '$req1', '$req2', '$req3', '$req4', '$req5', '$req6', '$req7', '$Account_No', '$First_Name', '$Last_Name', '$Phone', '$ShipTo_Address', '$ShipTo_City', '$ShipTo_State', '$ShipTo_ZIP', '$Additional_Info', '$Accept') ";
$result = mysql_db_query($db,"$SQL",$cid);
}
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
echo ("<center><h1>Thank You.</h1></center>\n");
echo ("<center>Please use back button to go back.</center>\n");
}
mysql_close($cid); 
?>
</HTML>

Our company does fullfillment requests for a client, and this is the script that places the data into the mysql table from the form they fill out.

What I'm trying to add is a way to check the db before adding the record to prevent duplicate records being submitted to the db.

I've got the code in there, but it seems to fail at:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Accounts\national\wwwRoot\test\new_datain.php on line 52

I'm not sure what is wrong. Perhaps a syntax error. I'm new to php and mysql.

Thanks for any help you can provide me.

- jason

Member Avatar for fatihpiristine

this is integer right? then u cannot assign integer as text

change this :
Account_No = '$Account_No'"
to this:
Account_No = $Account_No"

it ll work

I wil try with:
if mysql_query("SELECT * FROM test WHERE Account_No = ".$Account_No)

If it don`t work.
Try with:
if mysql_query("sql", $cid)

ch.-

<HTML>
<?php
if (empty($CSP_Name) || empty($Supervisor) || empty($Rep_Number) || empty($Department) || empty($Account_No) || empty($First_Name) || empty($Last_Name) || empty($Phone) || empty($ShipTo_Address) || empty($ShipTo_City) || empty($ShipTo_State) || empty($ShipTo_ZIP) || empty($Accept))
{echo "<h2>Please make sure you have filled in all required fields. Please use your browsers Back button</h2>\n";
 echo "<h2>******REQUEST WAS NOT SUBMITTED******</h2>\n";}
if (isset ($_POST['chk8'])) {
$req1 = "Pre Band Mailer\n";
} else {
$req1 = "None\n";
}
if (isset ($_POST['chk9'])) {
$req2 = "At Brand Mailer\n";
} else {
$req2 = "None\n";
}
if (isset ($_POST['chk10'])) {
$req3 = "Voice Welcome Kit\n";
} else {
$req3 = "None\n";
}
if (isset ($_POST['chk11'])) {
$req4 = "Welcome Kit (Scientific Atlanta)\n";
} else {
$req4 = "None\n";
}
if (isset ($_POST['chk12'])) {
$req5 = "Three Product Welcome Kit (Motorola)\n";
} else {
$req5 = "None\n";
}
if (isset ($_POST['chk13'])) {
$req6 = "...\n";
} else {
$req6 = "None\n";
}
if (isset ($_POST['chk14'])) {
$req7 = "...\n";
} else {
$req7 = "None\n";
}
$ShipTo_Address = stripcslashes($ShipTo_Address);
$Additional_Info = stripcslashes($Additional_Info);
?>
<?php
$usr = "national_twc";
$pwd = "timewarnercable";
$db = "national_test";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
if ($REQUEST_METHOD=="POST") {
if (mysql_query("SELECT * FROM twc WHERE Account_No = $Account_No")) {
echo ("<center><h1>Sorry. Your account is already added.</h1></center>\n");
}    else {
$SQL = " INSERT INTO twc ";
$SQL = $SQL . " (DATE, CSP_Name, Supervisor, Rep_Number, Department, Email, req1, req2, req3, req4, req5, req6, req7, Account_No, First_Name, Last_Name, Phone, ShipTo_Address, ShipTo_City, ShipTo_State, ShipTo_ZIP, Additional_Info, Accept) VALUES ";
$SQL = $SQL . " (now(), '$CSP_Name', '$Supervisor', '$Rep_Number', '$Department', '$Email', '$req1', '$req2', '$req3', '$req4', '$req5', '$req6', '$req7', '$Account_No', '$First_Name', '$Last_Name', '$Phone', '$ShipTo_Address', '$ShipTo_City', '$ShipTo_State', '$ShipTo_ZIP', '$Additional_Info', '$Accept') ";
$result = mysql_db_query($db,"$SQL",$cid);
}
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
echo ("<center><h1>Thank You.</h1></center>\n");
echo ("<center>Please use back button to go back.</center>\n");
}
mysql_close($cid); 
?>
</HTML>

Thanks for your help. I've believe I've changed what you told me to correctly. I'm posting my code again for you to double check me and point me in the right direction.

It seems its passing the check and the error message, and just adding the record to the db. Am I missing something? Please help me.


Thanks again

Jason

Member Avatar for fatihpiristine

you get empty record or you get full data you entered into fields. ??

I get a record entered with data each time. It does not check to see if the account number exists before adding it to the db.

Thanks

Jason

On line 52, you are not select the DB yet.

mysql_select_db($db);

ch.-

Member Avatar for fatihpiristine

dump your table into text document n zip it with this page n upload here.. i ll check it...

Starting from line 52:

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM `twc` WHERE `Account_No` = ".$Account_No),0) or die(mysql_error());

if ($total_results > 0) {
// if one or more rows are returned an account with this id exists
echo ("<center><h1>Sorry. Your account is already added.</h1></center>\n");

} else {
//no rows were returned so we can add data

First you need to query the db and see if you get a result from the query. I'm not sure but the best I can remember the way it was would be true as the query would return true even if no rows matched your terms. You may also want to check to make sure that the Account_No variable is being processed correctly and passed correctly, this most likely where your problem lies as without this var being correct you will never get a correct query and all data would be added like your talking about. For testing you could echo $Account_No; just before its used in the query

okay, I think I'm making head way with this little project. Thanks to all of you who have helped and guided me. I do appreciate it.

I have changed and update my code to include selecting the db and the SELECT statement.

Now when I execute it, I receive the following error:


Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\Accounts\national\wwwRoot\new_datain.php on line 53
Access denied for user: 'national_twc@localhost' to database 'national_test'

I'll post my updated code here for you to review.

Once again thanks for all your help! I'll look forward to hearing from anyone.

<HTML>
<?php
if (empty($CSP_Name) || empty($Supervisor) || empty($Rep_Number) || empty($Department) || empty($Account_No) || empty($First_Name) || empty($Last_Name) || empty($Phone) || empty($ShipTo_Address) || empty($ShipTo_City) || empty($ShipTo_State) || empty($ShipTo_ZIP) || empty($Accept))
{echo "<h2>Please make sure you have filled in all required fields. Please use your browsers Back button</h2>\n";
 echo "<h2>******REQUEST WAS NOT SUBMITTED******</h2>\n";}
if (isset ($_POST['chk8'])) {
$req1 = "Pre Band Mailer\n";
} else {
$req1 = "None\n";
}
if (isset ($_POST['chk9'])) {
$req2 = "At Brand Mailer\n";
} else {
$req2 = "None\n";
}
if (isset ($_POST['chk10'])) {
$req3 = "Comcast Digital Voice Welcome Kit\n";
} else {
$req3 = "None\n";
}
if (isset ($_POST['chk11'])) {
$req4 = "Three Product Welcome Kit (Scientific Atlanta)\n";
} else {
$req4 = "None\n";
}
if (isset ($_POST['chk12'])) {
$req5 = "Three Product Welcome Kit (Motorola)\n";
} else {
$req5 = "None\n";
}
if (isset ($_POST['chk13'])) {
$req6 = "...\n";
} else {
$req6 = "None\n";
}
if (isset ($_POST['chk14'])) {
$req7 = "...\n";
} else {
$req7 = "None\n";
}
$ShipTo_Address = stripcslashes($ShipTo_Address);
$Additional_Info = stripcslashes($Additional_Info);
?>
<?php
$usr = "national_twc";
$pwd = "timewarnercable";
$db = "national_test";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
if ($REQUEST_METHOD=="POST") {
mysql_select_db($db);
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM `twc` WHERE `Account_No` = ".$Account_No),0) or die(mysql_error());
if ($total_results > 0) {
// if one or more rows are returned an account with this id exists
echo ("<center><h1>Sorry. Your account is already added.</h1></center>\n");
} else {
//no rows were returned so we can add data
$SQL = " INSERT INTO twc ";
$SQL = $SQL . " (DATE, CSP_Name, Supervisor, Rep_Number, Department, Email, req1, req2, req3, req4, req5, req6, req7, Account_No, First_Name, Last_Name, Phone, ShipTo_Address, ShipTo_City, ShipTo_State, ShipTo_ZIP, Additional_Info, Accept) VALUES ";
$SQL = $SQL . " (now(), '$CSP_Name', '$Supervisor', '$Rep_Number', '$Department', '$Email', '$req1', '$req2', '$req3', '$req4', '$req5', '$req6', '$req7', '$Account_No', '$First_Name', '$Last_Name', '$Phone', '$ShipTo_Address', '$ShipTo_City', '$ShipTo_State', '$ShipTo_ZIP', '$Additional_Info', '$Accept') ";
$result = mysql_db_query($db,"$SQL",$cid);
}
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
echo ("<center><h1>Thank You.</h1></center>\n");
echo ("<center>Please use back button to go back.</center>\n");
}
mysql_close($cid); 
?>
</HTML>

Did you check the schema privileges assigned to the user 'national_twc' from 'localhost' and the access avaible to the database 'national_test'?
If it possible, you can use the 'root' user to test the script.

ch.-

Thanks for the solutions.. I created a new account with all the rights my host would allow me, and now when I execute the script, it comes up with a blank page, and no data is entered into the db. I'm lost now. Please help.

- jason

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.