Page 1 of 1

PHP newbie help needed

Posted: Wed May 21, 2014 8:55 am
by flashybee
I just follow one php tutorials wherein there is a html form and when the "ok" button will be pressed the insert query will be printed. 'Though I follow the tutorials it will not do the same as expected. Do anybody have some time to look into the code where I am gong wrong. I am new in PHP.

Code: Select all

    <?php

    include("mySqlConnect.php");

    mysql_select_db($dbname,$testConnect)  or die (mysql_error());
    // creating table

    /*$sql= "CREATE TABLE `user`.`example`
                 (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
             `firstname` VARCHAR(64) NOT NULL,
             
             `lastname` VARCHAR(64) NOT NULL,
              `region_name` INT ,
             `ssnumber` INT ,
             `age` INT)
              ENGINE = MyISAM;";
             mysql_query($sql) or die (mysql_error());
             print "Table Created  Successfully";
             */
             
             // inserting values in the table
             
             
             
       if(isset($function)&&$function=="add"){   
       print("This is the value");
          
             
    $sqlInsert = "INSERT INTO  'user' . `example`(
                      firstName,
                      lastName,
                      region_name,
                      ssnumber,
                      age)
                      VALUES ('Tarun', 'Ghosh', 1, 44,54)";

                         
                   print("$sqlInsert<br>");
                         
                

    //mysql_query($sqlInsert) or die (mysql_error());
    }

    $sqlReg = "SELECT regionName,regionNumber FROM  regions";
    $Result = mysql_query($sqlReg,$testConnect) or die (mysql_error());

        /*$row_nameCheck =mysql_fetch_assoc($Result);

       $firstNameValue = $row_nameCheck['regionName'];
       $middleNameValue = $row_nameCheck['regionNumber'];
       

        echo "This is the First Name:- $firstNameValue <br>";
        echo "This is the MIddle Name :- $middleNameValue <br>";*/
    //$myrow = mysql_fetch_array($Result);
    //print_r("$Result");

    //echo $myrow['regionNumber'];

    //print_r("$Result");


    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>

    <form id="enterForm" name="dateEnter" method="post" action="index.php?function=add">
    Enter First Name: <input type="text" name="name" id="name"/><br />
    Enter Last Name : <input type="text" name="lastname" id="lastname"/><br />
    Select Region Number:
    <select name="regionNumber"id="regionNumber">
     
     
         <option>
       <?php while ($myRow = mysql_fetch_array($Result)){ ?>
       <option value="<?php echo $myRow['regionNumber'];?>"><?php echo $myRow['regionNumber']. "-" .$myRow['regionName']; ?></option>
       
       <?php } ?>
       
       
       </option>

    </select>
    <br/>
    Please Enter SS Number No Dashes:
    <input type="text" name="ssnumber" id="ssnumber"/><br/>
    Enter your age : <input type="text" name="age" id="age"/><br />
    <input type="submit" name="addData" id="addData" value="Ok" />

    </form>
    </body>
    </html>


  
If the code is not so clear I will redo the same for your look.

Thanks.

Re: PHP newbie help needed

Posted: Wed May 21, 2014 10:19 pm
by flashybee
Shall be glad if I get any kind of response.

Thanks.

Re: PHP newbie help needed

Posted: Wed May 21, 2014 10:26 pm
by Celauran
Your print statement on line 38 isn't being reached because it's checking for the existence of $function, which isn't set. Based on your form action, you probably want $_GET['function'] instead.

Whatever tutorial you're following that's telling you to do this needs to be ignored.

Code: Select all

    mysql_select_db($dbname,$testConnect)  or die (mysql_error());

Re: PHP newbie help needed

Posted: Thu May 22, 2014 9:40 am
by flashybee
It's a video tutorials from cartoonsmart.com so what will be the right approach to learn php or can you give me some reference site to learn the right syntax to use. May be I am asking too much due to my ignorance , but I like to learn. Instead of this one:- mysql_select_db($dbname,$testConnect) or die (mysql_error()); what will be the code to follow.

Thanks for the reply.

Re: PHP newbie help needed

Posted: Thu May 22, 2014 1:26 pm
by Celauran
It's not a question of syntax. mysql_ functions have been deprecated. You should be using PDO. Recommending mysql_query was fine in 2004. Not anymore. You also really shouldn't ever use die(). Handle errors when you can, don't ever expose them to the user. If it's something you can't recover from -- like your DB being down -- at least redirect your users to an error page. Don't just up and quit.

If you're just getting started with PHP, take a look at PHP: The Right Way.

Re: PHP newbie help needed

Posted: Thu May 22, 2014 10:17 pm
by flashybee
Thanks for the information, as I am new I just trying to learn through these old tutorials. Will try to use the site link you gave. Thanks.

Re: PHP newbie help needed

Posted: Sat May 24, 2014 1:01 pm
by flashybee
I need another help, while googled about php and mysql I came across with some articles wherein it has been described that the previous method of connecting to database is obsolete and it is time to use "mySqli" or "PDO". Both are new to me. Can anyone suggest any E- book to know which one is good to use and how to use them , and which one is better to use.

Thanks.

Re: PHP newbie help needed

Posted: Sat May 24, 2014 6:32 pm
by Celauran
See above. I already mentioned that. Use PDO; MySQLi is a pain. The PDO Manual at php.net is quite good.

Re: PHP newbie help needed

Posted: Sun May 25, 2014 12:42 am
by flashybee
Thanks for the help.