Search
Left Quote    It is possible to store the mind with a million facts and still be entirely uneducated.
- Alec Bourne    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: question about php used on tuts
  This user is offline  duece
  Subject: "question about php used on tuts" Posted: @ 1:07 am on Aug 10 2007   
Member #: 341
Rank: User - (14)
Since: 06/12/07
Posts: 14

okay... so looking at the member user code....
I see the following code is used to insert new members into the table...

Code:
$insert ="INSERT INTO `members` (username, user_password, user_email) VALUES ('".$_POST ['username'].&q uot;', '".md5($_P OST['password'] )."', '".$_POST[ 'email']." ')";

$insert2 = mysql_query($in sert);
if(!$insert2) die(mysql_error ());

echo('Registrat ion Successful, Welcome new member! You can now login to your new account.');


However on http://www.w3sc hools.com/php/p hp_mysql_insert .asp
is notes the following for inserting info from a form...

Code:
<?php
$con = mysql_connect(& quot;localhost& quot;,"pet er"," abc123";
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db ("my_db&qu ot;, $con);

$sql="INSE RT INTO person (FirstName, LastName, Age)
VALUES
('$_POST[firstn ame]','$_POST[l astname]','$_PO ST[age]')" ;
if (!mysql_query($ sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($co n)
?>



which is a little different. Is this insignificant or purposeful. I ask as I have expanded my form for the members tut and am having problems.
The Differences that I am noting and questioning:
(referring to the w3c sites as comparing to AllSytnax's tut)

the lack of ' ' around the table in the insert command,
the lack of ." " around the form data to inserted.
While I know there are tons of ways to skin a cat on the web I just wanted to know what the reason was for these inclusions..

Also here is the code I am having a problem with...





Code:
<?php
//CALL TO SERVER CONNECT FILE
require_once('F connect.php');

//SEE IF ALREADY LOGGED IN

if(isset($HTTP _POST_VARS['sub mit']))


//BEGIN USERNAME CHECKING
{
if(!$_POST['fir mname']) die('Alert: Firm Name field was blank.');


//array of invalid characters
$junk = array('.' , ',' , '/' , '\\' , '`' , ';' , '[' , ']' , '-',
'*', '&', '^', '%', '@', '!', '~', '+', '(', ')',
'|', '{', '}', '<', '>', '?', ':', '"', '=');


//starting lenght of username
$len = strlen($_POST[' username']);

//replace invalid characters
$_POST['usernam e'] = str_replace($ju nk, '', $_POST['usernam e']);
$test = $_POST['usernam e'];

//if lenghts are different ($len smaller), invalid characters found, prompt error.
if(strlen($test ) != $len) {
die('Username Error: Username contained invalid characters. You can only use A-Z, 0-9 and the
underscore (_).');
}


//Data Checks

if(!$_POST['fir mname']) {
die('Error: Firm Name field was blank');
}
if(!$_POST['fir mid']) {
die('Error: Firm ID field was blank.');
}



//ADD NEW MEMBER

$insert ="INSERT INTO `lender` (fName, fFId, fAddress, fCity, fState, fZip, fContact, fPhoneA, fExtension, fPhoneB, fEmail, fLicense, fBroker, fUpdate, fLeads, fLeft, fPrice, fActive)
VALUES ('".$_POST ['firmname'].&q uot;', '".($_POST ['firmid']).&qu ot;',
'".($_POST ['address']).&q uot;', '".($_POST ['city'])." ;', '".($_POST ['state']).&quo t;',
'".($_POST ['zip'])." ', '".($_POST ['contact']).&q uot;', '".($_POST ['phoneA']).&qu ot;',
'".($_POST ['extension']). "', '".($_POST ['phoneB']).&qu ot;', '".($_POST ['email']).&quo t;',
'".($_POST ['license']).&q uot;', '".$_POST[ 'brokers'].&quo t;'), '".($_POST ['update']).&qu ot;',
'".($_POST ['leads']).&quo t;', '".$_POST[ 'left']."' ), '".($_POST ['price']).&quo t;', '".($_POST ['active']).&qu ot;')";


$insert2 = mysql_query($in sert);
if(!$insert2) {
die(mysql_error ());
echo('Your Registration was NOT-Successful' );
}


//Successful Addition
echo('Welcome "'.$q3-> ;fAdmin.'" Your Registration was Successful, Now You can
<a href="http ://localhost/eF inancial/Web/Fr egister.php&quo t;>Register& lt;/a> another account.');

} else {
?>
<!-- Entry Form -->
<style type="text /css">
<!--
.style3 {font-size: 12px}
-->
</style>


<table width="456 ">
<!--DWLayout Table-->
<form name="sign up" action="&l t;?php $_SERVER['PHP_S ELF']; ?>" method="PO ST">
<tr>
<td height="42 " colspan="2 " valign="to p">< p align="cen ter">Re gistration Page </p>
<p align="cen ter">&l t;span class="sty le3">(o nly A-Z, 0-9 and _ Allowed)</sp an><span class="sty le3">&l t;br />
</span>&l t;BR>
</p></ td>
</tr>
<tr>
<td height="42 " colspan="2 " valign="to p">< div align="cen ter">&l t;br />
Firm Information </div>< ;/td>
</tr>
<tr>
<td width="185 " height="42 " valign="to p">Firm Name </td>
<td width="255 " valign="bo ttom">& lt;input type="text " id ="firmname " name="firm name" value="&qu ot; maxlength=" ;30" /></td> ;
</tr>
<tr>
<td height="50 " valign="to p">Firm ID <br />
<span class="sty le3">1s t 2 letters of name + # of Brokers</spa n> </td>
<td valign="to p">< input type="text " id="firmid " name="firm id" value="&qu ot; maxlength=" ;10">
<BR>
(minimum 3 characters)< /td>
</tr>
<tr>
<td height="32 " valign="to p">Addr ess</td>
<td valign="to p">< input type="text " id="addres s" name="addr ess" value="&qu ot; size="40&q uot; ></td>
</tr>
<tr>
<td height="32 " valign="to p">City </td>
<td valign="to p">< input type="text " id="city&q uot; name="city " value="&qu ot; size="30&q uot; /> </td>
</tr>
<tr>
<td height="32 " valign="to p">Stat e<br /></td> ;
<td valign="to p">
<select name="stat e" id="state& quot; >
<option selected=" selected"& gt;select</o ption>
<option value="AL& quot;>Alabam a</option> ;
<option value="AK& quot;>Alaska </option>
<option value="AZ& quot;>Arizon a</option> ;
<option value="AR& quot;>Arkans as</option&g t;
<option value="CA& quot;>Califo rnia</option >
<option value="CO& quot;>Colora do</option&g t;
<option value="CT& quot;>Connec ticut</optio n>
<option value="DE& quot;>Delawa re</option&g t;
<option value="DC& quot;>Distri ct of Columbia</op tion>
<option value="FL& quot;>Florid a</option> ;
<option value="GA& quot;>Georgi a</option> ;
<option value="HI& quot;>Hawaii </option>
<option value="ID& quot;>Idaho& lt;/option>
<option value="IL& quot;>Illino is</option&g t;
<option value="IN& quot;>Indian a</option> ;
<option value="IA& quot;>Iowa&l t;/option>
<option value="KS& quot;>Kansas </option>
<option value="KY& quot;>Kentuc ky</option&g t;
<option value="LA& quot;>Louisi ana</option& gt;
<option value="ME& quot;>Maine& lt;/option>
<option value="MD& quot;>Maryla nd</option&g t;
<option value="MA& quot;>Massac husetts</opt ion>
<option value="MI& quot;>Michig an</option&g t;
<option value="MN& quot;>Minnes ota</option& gt;
<option value="MS& quot;>Missis sippi</optio n>
<option value="MO& quot;>Missou ri</option&g t;
<option value="MT& quot;>Montan a</option> ;
<option value="NE& quot;>Nebras ka</option&g t;
<option value="NV& quot;>Nevada </option>
<option value="NH& quot;>New Hampshire</o ption>
<option value="NJ& quot;>New Jersey</opti on>
<option value="NM& quot;>New Mexico</opti on>
<option value="NY& quot;>New York</option >
<option value="NC& quot;>North Carolina</op tion>
<option value="ND& quot;>North Dakota</opti on>
<option value="OH& quot;>Ohio&l t;/option>
<option value="OK& quot;>Oklaho ma</option&g t;
<option value="OR& quot;>Oregon </option>
<option value="PA& quot;>Pennsy lvania</opti on>
<option value="RI& quot;>Rhode Island</opti on>
<option value="SC& quot;>South Carolina</op tion>
<option value="SD& quot;>South Dakota</opti on>
<option value="TN& quot;>Tennes see</option& gt;
<option value="TX& quot;>Texas& lt;/option>
<option value="UT& quot;>Utah&l t;/option>
<option value="VT& quot;>Vermon t</option> ;
<option value="VA& quot;>Virgin ia</option&g t;
<option value="WA& quot;>Washin gton</option >
<option value="WV& quot;>West Virginia</op tion>
<option value="WI& quot;>Wiscon sin</option& gt;
<option value="WY& quot;>Wyomin g</option> ;
</select>
</span>&l t;/td>
</tr>
<tr>
<td height="40 " valign="to p">Zip Code <br /></td> ;
<td valign="to p">< input type="text " id="zip&qu ot; name="zip& quot; value="&qu ot; >
<br></ td>
</tr>
<tr>
<td height="40 " colspan="2 " valign="to p">< div align="cen ter">&l t;br />
Contact Information </div>< ;/td>
</tr>
<tr>
<td height="40 " valign="to p">Main Contact Name </td>
<td valign="to p">< input type="text " id="contac t" name="cont act" value="&qu ot;></td& gt;
</tr>
<tr>
<td height="40 " valign="to p">Phon e Number A </td>
<td valign="to p">< input type="text " id="phoneA " name="phon eA" value="&qu ot; /></td> ;
</tr>
<tr>
<td height="40 " valign="to p">Exte nsion</td> ;
<td valign="to p">< input type="text " id="extens ion" name="exte nsion" value="&qu ot;></td& gt;
</tr>
<tr>
<td height="40 " valign="to p">Phon e Number B </td>
<td valign="to p">< input type="text " id="phoneB " name="phon eB" value="&qu ot;></td& gt;
</tr>
<tr>
<td height="40 " valign="to p">Emai l</td>
<td valign="to p">< input type="text " id="email& quot; name="emai l" value="&qu ot;></td& gt;
</tr>
<tr>
<td height="40 " valign="to p">Stat e Licenses<br />
<span class="sty le3">se lect states <strong>n ot</strong&g t; licensed </span> </td>
<td valign="to p">
<select name="lice nse" id="licens e" size="4&qu ot; multiple=" multiple" >
<option selected=" selected"& gt;select</o ption>
<option value="AL& quot;>Alabam a</option> ;
<option value="AK& quot;>Alaska </option>
<option value="AZ& quot;>Arizon a</option> ;
<option value="AR& quot;>Arkans as</option&g t;
<option value="CA& quot;>Califo rnia</option >
<option value="CO& quot;>Colora do</option&g t;
<option value="CT& quot;>Connec ticut</optio n>
<option value="DE& quot;>Delawa re</option&g t;
<option value="DC& quot;>Distri ct of Columbia</op tion>
<option value="FL& quot;>Florid a</option> ;
<option value="GA& quot;>Georgi a</option> ;
<option value="HI& quot;>Hawaii </option>
<option value="ID& quot;>Idaho& lt;/option>
<option value="IL& quot;>Illino is</option&g t;
<option value="IN& quot;>Indian a</option> ;
<option value="IA& quot;>Iowa&l t;/option>
<option value="KS& quot;>Kansas </option>
<option value="KY& quot;>Kentuc ky</option&g t;
<option value="LA& quot;>Louisi ana</option& gt;
<option value="ME& quot;>Maine& lt;/option>
<option value="MD& quot;>Maryla nd</option&g t;
<option value="MA& quot;>Massac husetts</opt ion>
<option value="MI& quot;>Michig an</option&g t;
<option value="MN& quot;>Minnes ota</option& gt;
<option value="MS& quot;>Missis sippi</optio n>
<option value="MO& quot;>Missou ri</option&g t;
<option value="MT& quot;>Montan a</option> ;
<option value="NE& quot;>Nebras ka</option&g t;
<option value="NV& quot;>Nevada </option>
<option value="NH& quot;>New Hampshire</o ption>
<option value="NJ& quot;>New Jersey</opti on>
<option value="NM& quot;>New Mexico</opti on>
<option value="NY& quot;>New York</option >
<option value="NC& quot;>North Carolina</op tion>
<option value="ND& quot;>North Dakota</opti on>
<option value="OH& quot;>Ohio&l t;/option>
<option value="OK& quot;>Oklaho ma</option&g t;
<option value="OR& quot;>Oregon </option>
<option value="PA& quot;>Pennsy lvania</opti on>
<option value="RI& quot;>Rhode Island</opti on>
<option value="SC& quot;>South Carolina</op tion>
<option value="SD& quot;>South Dakota</opti on>
<option value="TN& quot;>Tennes see</option& gt;
<option value="TX& quot;>Texas& lt;/option>
<option value="UT& quot;>Utah&l t;/option>
<option value="VT& quot;>Vermon t</option> ;
<option value="VA& quot;>Virgin ia</option&g t;
<option value="WA& quot;>Washin gton</option >
<option value="WV& quot;>West Virginia</op tion>
<option value="WI& quot;>Wiscon sin</option& gt;
<option value="WY& quot;>Wyomin g</option> ;
</select>
</span>&l t;/td>
</tr>
<tr>
<td height="40 " valign="to p">Numb er of Brokers </td>
<td valign="to p">< input type="text " id="broker " name="brok er" value="&qu ot; /></td> ;
</tr>
<tr>
<td height="40 " colspan="2 " valign="to p">< div align="cen ter">&l t;br />
Lead Package Information </div>< ;/td>
</tr>
<tr>
<td height="40 " valign="to p">Date last Updated </td>
<td valign="to p">< input type="text " id="update " name="upda te" value="&qu ot; /></td> ;
</tr>
<tr>
<td height="40 " valign="to p">Numb er of Leads Purchsed </td>
<td valign="to p">< input type="text " id="leads& quot; name="lead s" value="&qu ot; /></td> ;
</tr>
<tr>
<td height="40 " valign="to p">Lead s left </td>
<td valign="to p">< input type="text " id="left&q uot; name="left " value="&qu ot; /></td> ;
</tr>
<tr>
<td height="40 " valign="to p">Pric e</td>
<td valign="to p">< input type="text " id="price& quot; name="pric e" value="&qu ot; /></td> ;
</tr>
<tr>
<td height="40 " valign="to p">Firm Active</td&g t;
<td valign="to p">< input type="text " id="active " name="acti ve" value="&qu ot; /></td> ;
</tr>
<tr>
<td height="40 " valign="to p">< !--DWLayoutEmpt yCell-->& ;nbsp;</td&g t;
<td valign="to p">< !--DWLayoutEmpt yCell-->& ;nbsp;</td&g t;
</tr>
<tr>
<td height="21 " colspan="2 " valign="to p">< !--DWLayoutEmpt yCell-->& ;nbsp;</td&g t;
</tr>
<tr>
<td height="56 " valign="to p"> Complete Firm Registration:&l t;/td>
<td valign="to p">< input type="subm it" id="submit " name="subm it" value="sub mit">
<br />
<input type="hidd en" name="subm itted" value="TRU E" /> <!--used for email verification --> </td>
</tr>
</form>
</table>
<?php

} //end submit not pressed
?>
/code]


Ohh yeah... the error...
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 6

Thanks
D

Edited at 01:27:19 am on 08/10/07


    Viewed: 7,466 Times | Reply to This | To top
  This user is offline  duece
  Subject: "re: question about php used on tuts" Posted: @ 3:26 am on Aug 10 2007    
Member #: 341
Rank: User - (14)
Since: 06/12/07
Posts: 14

okay so I have come to realize that the error I am getting is because I have xx # of fields in my datatbase and only x fields that I am inserting data in...

so then my next question is this...

if the format of the insert as it is shown on your tuts


Code:

$insert ="INSERT INTO `members` (username, user_password, user_email) VALUES ('".$_PO ST['username' ]."', '".md5($ _POST['passwor d'])."' , '".$_POS T['email'].& quot;')" ;


is correct and I want to add nothing for this particular insert into the fields masterID, lastlogin and lastchange would it be like so...


Code:
$insert ="INSERT INTO `members` (masterid, username, user_password, user_email, lastlogin, lastchange ) VALUES ('', '".$_POS T['username'] ."', '".md5($ _POST['passwor d'])."' , '".$_POS T['email'].& quot;, '', '')";



or would it have to include the extra "." that the others show... I guess it is still showing that I don't get the stuff between the '' on some of them... I'm a nube... so shoot me I guess... but you were once too... if you have any other good links, books or anything to recommend I am totally open. I am still waiting for two in the mail... ordered em on half.com twice as apparently people like to post things on there they don't really have... any way thanks for the help...
D





    Viewed: 7,453 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: question about php used on tuts" Posted: @ 5:52 pm on Aug 10 2007    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

You don't have to insert data into every column of the table if you don't want to. Just specify the columns you want to add data to, followed by the variables that contain the data you want inserted.

$q = mysql_query(&q uot;INSERT INTO `table` (column1, column2, column3, column4) VALUES (column1_var, column2_var, column3_var, column4_var)&q uot;;

the ` and ` around table name and fields is optional I believe. PHP.NET is the best resource you need for PHP. If you have a question about a function, you can get info on it by going to PHP.NET/functio nName - takes you right there. So if you're looking for info on the "echo&qu ot; function, the URL would be: http://www.php. net/echo




-bs0d | AllSyntax.com

    Viewed: 7,438 Times | Reply to This | To top
Viewing Page: 1 of 1


1 |

You must be logged in to post on the forums. Login or Register








"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank