Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
| Search | Today's Posts | Mark Forums Read
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 27th, 2007, 06:57 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to asalaam Send a message via Yahoo to asalaam
Default how to insert values in tables using php??

hi all,i'm very new in php :D.so i copied these code from Wrox BegPHP5 book..and implement as a testing but this s not given me the result when i put all the fileds for user registration..why??..the code s here
//include file. common_db.inc
<?php
 $dbhost = 'localhost';
 $dbusername = 'phpuser';
 $dbuserpassword = 'phppass';
 $default_dbname = 'sample_db';

 $records_per_page = 5;

 $user_tablename = 'user';
 $access_log_tablename = 'access_log';

 $MYSQL_ERRNO = '';
 $MYSQL_ERROR = '';

 $new_win_width = 600;
 $new_win_height = 400;
 html_header();

 function html_header()
 {
 global $new_win_width, $new_win_height;
  ?>
  <HTML>
  <HEAD>
  <SCRIPT LANGUAGE="JavaScript" type="text/javascript" >
  <!--
  function open_window(url)
  {
  var NEW_WIN = null;
  NEW_WIN =window.open("" ,"RecordViewer","toolbar=no,width="+ <?php echo $new_win_width ?> + ",directories=no,status=no"+",directories=no, status=no"+", scrollbars=yes,resize=no,menubar=no");
  NEW_WIN.location.href = url;
  }
  //-->
  </script>
  <title>User Record Viewer</title>
  <head></head>
  <body>
  <?php
  }

  function html_footer()
  {
  ?>
  </body>
  </html>
  <?php
  }

 function db_connect()
 {
 global $dbhost,$dbusername,$dbuserpassword,$default_dbnam e;
 global $MYSQL_ERRNO,$MYSQL_ERROR;
 $link_id = mysql_connect($dbhost, $dbusername,$dbuserpassword);
 if(!$link_id)
 {
 $MYSQL_ERRNO = 0;
 $MYSQL_ERROR = "Connection Failed to the host $dbhost";
 return 0;
 }
 else if(empty($dbname) && !mysql_select_db($default_dbname))
 {
 $MYSQL_ERRNO = mysql_errno();
 $MYSQL_ERROR = mysql_error();
 return 0;
 }
 else return $link_id;
 }
 function sql_error()
 {
  global $MYSQL_ERRNO,$MYSQL_ERROR;
  if(empty($MYSQL_ERROR))
  {
  $MYSQL_ERRNO = mysql_errno();
  $MYSQL_ERROR = mysql_error();
  }
  return "$MYSQL_ERRNO:$MYSQL_ERROR";
  }

  function error_message($msg)
  {
  html_header();
  echo "<script>alert(\"Error:$msg\");history.go(-1)</script>";
  html_footer();
  exit;
  }
  function enum_options($field, $link_id) {
   $query = "SHOW COLUMNS FROM user LIKE '$field'";
   $result = mysql_query($query, $link_id);
   $query_data = mysql_fetch_array($result);

   if(eregi("('.*')", $query_data["Type"], $match)) {
      $enum_str = ereg_replace("'", "", $match[1]);
      $enum_options = explode(',', $enum_str);
      return $enum_options;
   } else return 0;
}
 ?>
and register.php code here.......
<?php
include "./common_db.inc";
function in_use($userid)
{
$query = "SELECT userid FROM $user_tablename WHERE userid= '$userid' ";
$result = mysql_query($query);
if (!mysql_num_rows($result))
{
return 0;
}
else
{
return 1;
}
}
function register_form ()
{
global $userposition;
global $PHP_SELF;
$link_id = db_connect();
mysql_select_db ("sample_db");
$position_array = enum_options('userposition',$link_id);
mysql_close($link_id);

?>
<center><h3>Create Your Account !</h3></center>
<form method="post" action="<?php echo $PHP_SELF ?> " >
<input type="hidden" name="action" />
<div align="center"><center><table border="1" width="90%" >
    <tr>
        <th width="30%" nowrap="nowrap">Desired ID </th>
        <td width="70%" ><input type="text" name="userid" size="8" maxlength="8" /></td>
    </tr>

    <tr>
        <th width="30%" nowrap="nowrap">Desire Password </th>
        <td width="70%" ><input type="password" name="userpassword" size="15" /></td>
    </tr>

    <tr>
        <th width="30%" nowrap="nowrap">Retype Password </th>
        <td width="70%"><input type="password" name="userpassword2" size="15" /></td>
    </tr>

    <tr>
        <th width="30%" nowrap="nowrap">Full Name</th>
        <td width="70%" ><input type="text" name="username" size="20" /></td>
    </tr>

    <tr>
        <th width="30%" nowrap="nowrap">Position</th>
        <td width="70%"><select name="userposition" size="1" >

<?php
     for($i=0; $i < count($position_array); $i++)
 {
    if(! isset($userposition) && $i== 0)
 {
 echo "<OPTION SELECTED VALUE=\"" .$position_array[$i]. "\" >" .$position_array[$i] . "</OPTION>\n";
 }

 else if($userposition == $cposition_array[$i])
 {
 echo "<OPTION SELECTED VALUE =\"" .$position_array[$i]. "\" >" .$position_array[$i] . "</OPTION>\n";
 }
 else
 {
 echo "<OPTION VALUE = \" " . $position_array[$i] . "\"> " .$position_array[$i] . "</OPTION> \n";
 }
 }
 ?>
 </select></td>
 </tr>
         <tr>
            <th width="30%" nowrap="nowrap">Email</th>
            <td width="70%"><input type="text" name="useremail" size="20" /></td>
        </tr>

        <tr>
            <th width="30%" nowrap="nowrap">Profile</th>
            <td width="70%"><textarea rows="5" cols="40" name="userprofile" ></textarea>
            </td>
        </tr>

        <tr>
                <th width="30%" colspan="2" nowrap="nowrap">
                <input type="submit" value="Submit" >
                <input type="reset" value="Reset" /></th>
        </tr>
    </table>
    </center>
    </div>
    </form>
    <?php
    }

     function create_account()
     {
     $userid = $_POST['userid'];
     $username = $_POST['username'];
     $userpassword = $_POST['userpassword'];
     $userpassword2 = $_POST['userpassword2'];
     $userposition = $_POST['userposition'];
     $useremail = $_POST['useremail'];
     $userprofile = $_POST['userprofile'];

     global $default_dbname, $user_tablename;

     if(empty($userid)) error_message("Enter Your Desire ID!");

     if(empty($username)) error_message ("Enter your Name !");

     if(strlen($userpassword) < 4 ) error_message("Password is too Short!");

     if(empty($userpassword2)) error_message("Enter your password for Verification!");

     if(empty($userposition)) error_message("Please fill your Position!");

     if (empty($useremail)) error_message("Give your E-mail ID!");

     if(empty($userprofile)) $userprofile = "No Comment.";

     if($userpassword2 != $userpassword) error_message ("Your Retype password and desired password mismatch");

     $link_id = db_connect($default_dbname);

     if(in_use($userid)) error_message ("Please choose another ID $userid is already in use!");

     $query = "INSERT INTO user VALUES(NULL, '$userid', password('$userpassword'), '$username', '$userposition',
                 '$useremail', '$userprofile')";

     $result = mysql_query($query);
     if(!$result) error_message(sql_error());

     $usernumber = mysql_insert_id($link_id);
     html_header();
     ?>
     <center><h3>
     <?php echo $username ?>, Welcome for Registering with us!
     </h3></center>
     <div align="center"><center><table border="1" width="90%">
     <tr>
         <th width="30%" nowrap="nowrap">User Number</th>
        <td width="70%"><?php echo $usernumber ?> </td>
        </tr>
     <tr>
         <th width="30%" nowrap="nowrap">Desired ID</th>
        <td width="70%"><?php echo $userid ?> </td>
        </tr>
    <tr>
         <th width="30%" nowrap="nowrap">User Password</th>
         <td width="70%"><?php echo $userpassword ?> </td>
         </tr>
    <tr>
          <th width="30%" nowrap="nowrap">Full Name</th>
          <td width="70%"><?php echo $username ?></td>
          </tr>
    <tr>
        <th width="30%" nowrap="nowrap">User Position</th>
        <td width="70%"><?php echo $userposition ?> </td>
        </tr>
    <tr>
        <th width="30%" nowrap="nowrap">User E-mail</th>
        <td width="70%"><?php echo $useremail ?> </td>
        </tr>
    <tr>
         <th width="30%" nowrap="nowrap">User Profile</th>
         <td width="70%"> <?php echo htmlspecialchars ($userprofile) ?> </td>
         </tr>
    </table>
    </center>
    </div>
    <?php
    html_footer();
    }

    if(empty($_POST)) $_POST['action'] = " ";
    switch ($_POST['action'])
    {
    case "register":
        create_account();
        break;
    default:
    html_header();
    register_form();
    html_footer();
break;
}
?>
now what i can do with dis??
  #2 (permalink)  
Old April 2nd, 2007, 01:31 PM
Sildaekar
Guest
 
Posts: n/a
Default

Do you have a MySQL database with the required tables uploaded?
  #3 (permalink)  
Old April 3rd, 2007, 05:05 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to asalaam Send a message via Yahoo to asalaam
Default

yea,i have.thanx 4 ur reply. i got it now.
  #4 (permalink)  
Old April 4th, 2007, 01:02 PM
Sildaekar
Guest
 
Posts: n/a
Default

ok...if you need anything else feel free to ask


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Data into two tables Lofa PHP Databases 4 July 7th, 2007 10:36 PM
how to insert values in tables using php?? asalaam SQL Server 2000 1 March 27th, 2007 11:23 AM
How to insert checkbox values to mysql using php method PHP Databases 0 February 21st, 2006 10:47 PM
Insert into Multiple Tables bmalex1 Beginning PHP 0 February 6th, 2006 12:45 PM
Insert into different Tables cs9896 SQL Language 2 September 16th, 2003 03:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.