Cant Locate Problem

Discussion in 'PHP' started by i m gr8, Dec 25, 2007.

  1. #1
    Hello,

    I am new to PHP and Mysql . I am working on an assignment I have been given . I had the choice to do in any language but I chose php because I ve always wanted to learn it .

    I am having problem submitting data collected from a form to the Mysql Database.

    The form is :

    
    
    <form action="personelprocess.php" method="POST">
    Name: <input type="text" name="name"><br>
    Age: <input type="text" name="age"><br>
    Marital Status: <input type="radio" name="maritalstatus" value="Married">Married <input type="radio" name="maritalstatus" value="Single"> Single<br>
    Military Courses: <input type="text" name="milcourses"><br>
    Civil Education: <input type="text" name="civedu"><br>
    Children: <input type="text" name="children"><br>
    Appt: <input type="text" name="appt"><br>
    Special Skills: <input type="text" name="specskills"><br>
    Sports: <input type="text" name="sports"><br>
    Home Address: <input type="text" name="homeaddress"><br>
    Blood Gp: <input type="text" name="bloodgp"><br>
    Religion: <input type="text" name="religion"><br>
    Sect: <input type="text" name="sect"><br>
    <input type="submit" value="Submit"><br>
    
    
    Code (markup):
    The personelprocess.php is:

    
    
    <?php
    
    $name=$_POST['name'];
    $age=$_POST['age'];
    $maritalstatus=$_POST['maritalstatus'];
    $milcourses=$_POST['milcourses'];
    $civedu=$_POST['civedu'];
    $children=$_POST['children'];
    $appt=$_POST['appt'];
    $specskills=$_POST['specskills'];
    $sports=$_POST['sports'];
    $homeaddress=$_POST['homeaddress'];
    $bloodgp=$_POST['bloodgp'];
    $religion=$_POST['religion'];
    $sect=$_POST['sect'];
    
    mysql_connect("localhost","*****","******") or die(mysql_error());
    mysql_select_db("****") or die(mysql_error());
    
    mysql_query("INSERT INTO personels (name,age,maritalstatus,milcourses,civedu,children,appt,specskills,sports,homeaddress,bloodgp,religion,sect) 
    VALUES ('$name','$age','$maritalstatus','$milcourses','$civedu','$children','$appt','$specskills','$sports','$homeaddress','$bloodgp','$religion','$sect')")
    or die(mysql_error());
    
    print "<p>Your information has been successfully added to the database</p>";
    
    ?>
    
    
    
    
    Code (markup):
    And the Mysql table for the above is :

    
    
    create table personels
    	(
    	id int(10) NOT NULL AUTO_INCREMENT,
    	name varchar(100) NOT NULL,
    	age int(4) NOT NULL,
    	maritalstatus varchar(10) NOT NULL,
    	milcourses varchar(255),
    	civedu varchar(255),
    	children int,
    	appt varchar(255),
    	specskills varchar(255),
    	sports varchar(255),
    	homeaddress varchar(255),
    	bloodgp varchar(5),
    	religion varchar(20),
    	sect varchar(20),
    	primary key (id)
    	);
    
    
    Code (markup):

    Can anyone kindly help me locate the problem?

    Thanks
     
    i m gr8, Dec 25, 2007 IP
  2. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What sort of problem? Can't it see the database? Is the query throwing an error?
     
    tamen, Dec 25, 2007 IP
  3. i m gr8

    i m gr8 Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    When I click submit on the form I get this 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 ''personels' (name,age,maritalstatus,milcourses,civedu,children,appt,specskills,s' at line 1
     
    i m gr8, Dec 25, 2007 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    What information are you sending?

    Is there a ' in any of the forms?
     
    HuggyStudios, Dec 25, 2007 IP
  5. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #5
    Use my standard form record insert:

    
    $array_donts = array('Submit');
    $query = array();
    $query2 = array();
    foreach($_POST as $is => $what){
    	if(!in_array($is,$array_donts)){ array_push($query," `$is` "); }
    	if(!in_array($is,$array_donts)){ array_push($query2," '".mysql_real_escape_string($what)."' "); }
    }
    $do_query = mysql_query("INSERT INTO `personels` (".join($query,',').") VALUES (".join($query2,',').")") or die(mysql_error());
    if($do_query){ $message = 'Your information has been successfully added to the database.';}else{$message = 'Unable to insert record.Internal Error. Please try again.';}
    
    PHP:
    Peace,
     
    Barti1987, Dec 25, 2007 IP
  6. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Your query looks ok, so I would suspect the data is corrupting the query. Have a look at the function mysql_real_escape_string - this will also sort out injection issues.

    I would also suggest assigning your query to a variable - that way you can echo the query to see what is wrong. Have a look at this code:

    <?php
    
    mysql_connect("localhost","*****","******") or die(mysql_error());
    mysql_select_db("****") or die(mysql_error());
    
    $name = mysql_real_escape_string( $_POST['name'] );
    $age = mysql_real_escape_string( $_POST['age'] );
    $maritalstatus = mysql_real_escape_string( $_POST['maritalstatus'] );
    $milcourses = mysql_real_escape_string( $_POST['milcourses'] );
    $civedu = mysql_real_escape_string( $_POST['civedu'] );
    $children = mysql_real_escape_string( $_POST['children'] );
    $appt = mysql_real_escape_string( $_POST['appt'] );
    $specskills = mysql_real_escape_string( $_POST['specskills'] );
    $sports = mysql_real_escape_string( $_POST['sports'] );
    $homeaddress = mysql_real_escape_string( $_POST['homeaddress'] );
    $bloodgp = mysql_real_escape_string( $_POST['bloodgp'] );
    $religion = mysql_real_escape_string( $_POST['religion'] );
    $sect = mysql_real_escape_string( $_POST['sect'] );
    
    $Query = "INSERT INTO personels (name,age,maritalstatus,milcourses,civedu,children,appt,specskills,sports,homeaddress,bloodgp,religion,sect) 
    VALUES ('$name','$age','$maritalstatus','$milcourses','$civedu','$children','$appt','$specskills','$sports','$homeaddress','$bloodgp','$religion','$sect')"
    
    mysql_query( $Query )
    or die(mysql_error());
    
    print "<p>Your information has been successfully added to the database</p>";
    
    ?>
    PHP:
    Brew
     
    Brewster, Dec 25, 2007 IP
  7. i m gr8

    i m gr8 Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hello,

    It did create 1 row when I tried this time but only the integer values got stored . The children and age column were stored. Age and children though both were stored as 0 while I didnt enter 0 .

    And it gave this errors:


    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 3

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 3

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 4

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 4

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 5

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 5

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 6

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 6

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 7

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 7

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 8

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 8

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 9

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 9

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 10

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 10

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 11

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 11

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 12

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 12

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 13

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 13

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 14

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 14

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'nobody'@'localhost' (using password: NO) in /home2/nicom/public_html/dbs/personelprocess.php on line 15

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home2/nicom/public_html/dbs/personelprocess.php on line 15

    Your information has been successfully added to the database

    Any idea?
     
    i m gr8, Dec 26, 2007 IP
  8. i m gr8

    i m gr8 Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hello Azizny,

    Could you kindly please explain this code to me a bit ? I am not understanding it . array_donts , array etc what are these? and also what is join doing ?

    Thanks
     
    i m gr8, Dec 26, 2007 IP
  9. i m gr8

    i m gr8 Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Used azizny code as is and is working perfectly fine.

    Thanks azizny , Could anyone or azizny explain it to me a bit . It also seems simpler in the sense if I gather 100s of things in one form I dont have to manually assign each field to a variable.
     
    i m gr8, Dec 26, 2007 IP
  10. i m gr8

    i m gr8 Peon

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I tried to modify the code such that I have both the form and process file in the same file so that if I make 10 forms on different pages I dont have to use 10 different process for each form. But I am getting an error this is the code:

    
    
    <?php
    $db_host="localhost";
    $db_user="********";
    $db_pass="*******";
    $db_name="*******";
    mysql_connect($db_host,$db_user,$db_pass) or die(mysql_error());
    mysql_select_db($db_name) or die(mysql_error());
    ?>
    
    <html>
    <title> Add a Personel</title>
    <body>
    
    <center>
    
    <h1>Add Personel</h1>
    
    <p>
    
    <?php
     
    if (isset($_POST['Submit']))
    	{
    
    ?>	
    
    	<form action="" method="POST">
    	Name: <input type="text" name="name"><br>
    	CNIC: <input type="text" name="cnic"><br>
    	Age: <input type="text" name="age"><br>
    	Marital Status: <input type="radio" name="maritalstatus" value="Married">Married <input type="radio" name="maritalstatus" value="Single"> Single<br>
    	Military Courses: <input type="text" name="milcourses"><br>
    	Civil Education: <input type="text" name="civedu"><br>
    	Children: <input type="text" name="children"><br>
    	Appt: <input type="text" name="appt"><br>
    	Special Skills: <input type="text" name="specskills"><br>
    	Sports: <input type="text" name="sports"><br>
    	Home Address: <input type="text" name="homeaddress"><br>
    	Blood Gp: <input type="text" name="bloodgp"><br>
    	Religion: <input type="text" name="religion"><br>
    	Sect: <input type="text" name="sect"><br>
    	<input type="submit" value="Submit"><br>
    
    <?php
    	}
    
    else
    	
    	{
    
    	$array_donts=array('Submit');
    	$query=array();
    	$query2=array();
    	
    	foreach ( $_POST as $is=>$what)
    		{
    		if (!in_array($is,$array_donts))
    			{
    			array_push($query,"$is");
    			}
    		if (!in_array($is,$array_donts))
    			{
    			array_push($query2," '".mysql_real_escape_string($what)."' ");
    			}
    		}
    
    	$query="INSERT INTO 'personels' (".join($query,',').") VALUES (".join($query2,',').");
    
    	$do_query=mysql_query($query) or die(mysql_error());
    
    	if ($do_query)
    
    		{
    		echo "Success!";
    		}
    	else
    		{
    		echo "Failure! Try Again !";
    		}
    	}
    ?>
    	
    
    </center>
    
    
    
    </body>
    </html>
    
    
    Code (markup):
    The error is :

    Parse error: syntax error, unexpected T_STRING in /home2/nicom/public_html/dbs/addpersonel.php on line 74

    By the way is there any compiler for php like for C++ we have Borland . It makes it easier to code in Borland for C++ as locating errors is easy . Writing everything in notepad and then uploading to check each time and manually going through everything is tough.
     
    i m gr8, Dec 26, 2007 IP