Current location: Hot Scripts Forums » Other Discussions » Database » Add column


Add column

Reply
  #1 (permalink)  
Old 07-14-09, 01:26 PM
zenix zenix is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Add column

Hello, I have been learning php and mysql on my own for the past few months now. I've received some very generous help from someone in here and I find myself stuck again. I have been struggling for about a week now on this and just can't seem to get something right. I would like to add a column to my database. I know to do this would be something like this:
ALTER TABLE testable1 ADD meeting VARCHAR(8);

Maybe I am putting it in the wrong place or something? Here is the code for the whole thing. I need to add a column named meeting (which I attempted to do by adding it to the code already present, so you'll see THAT. This, of course failed to add an actual column to the table.

Thank you in advance!!


Code:
<?php
session_start(); // Start or restart the session.
$sw=1;  // This variable used to toggle between the ADD and UPDATE buttons. Initally set to display the Add button.
$_SESSION["last_id"] = empty($_SESSION["last_id"]) ? 1 : $_SESSION["last_id"]; // This session variable used to store the current record id. It is set to 1 if there are no records in the database.
ini_set('display_errors', 1);
error_reporting(E_ALL &~E_NOTICE &~E_WARNING);
// Connect to the mysql server and create a database and a table if they don't already exist. //
$conn = mysql_connect('localhost', 'root', '')or die(mysql_error());


$date = date("F j, o"); 
$time = date("g:i a");  


$create = mysql_query("CREATE DATABASE IF NOT EXISTS db2") or die(mysql_error());
mysql_select_db ("db2");
$testdb = 'CREATE TABLE  IF NOT EXISTS testable1(id int (11) NOT NULL auto_increment,
                             lname varchar(40) NOT NULL,
                             fname varchar(40) NOT NULL,
                             address varchar(40) NOT NULL,
                             address2 varchar(40) NOT NULL,
                             city varchar(15) NOT NULL,
                             state varchar(2) NOT NULL,
                             zip int(5) NOT NULL,
                             phone varchar(11) NOT NULL,
                             notes longtext NOT NULL,
							 meeting varchar(10) NOT NULL,
                             PRIMARY KEY(id),
                             KEY testype(lname, fname))';


$result = mysql_query($testdb) or die(mysql_error());



// Check to see if the session array has been populated.
// If it hasn't, then fetch any records from the database and populate it.
if(empty($_SESSION["id"][0]))
{
 $results = mysql_query("select * from testable1") or die(mysql_error());
 while($r = mysql_fetch_assoc($results))
 {
  $_SESSION["id"][]=$_SESSION["last_id"]=$r["id"]; // $_SESSION["last_id"] will contain the last record id. If there are no records in the database, then it was previously set to 1.
  $_SESSION["lname"][]=$r['lname'];
  $_SESSION["fname"][]=$r['fname'];
  $_SESSION["address"][]=$r['address'];
  $_SESSION["address2"][]=$r['address2'];
  $_SESSION["city"][]=$r['city'];
  $_SESSION["state"][]=$r['state'];
  $_SESSION["zip"][]=$r['zip'];
  $_SESSION["phone"][]=$r['phone'];
  $_SESSION["notes"][]=$r['notes'];
  $_SESSION["metting"][]=$r['meeting'];
  $sw=0; // Display the Update button.
  }
 }
else{$sw=0;} // If the session array is already populated, then we need to display the Update button.

// Set the session array pointer to 0 to view the first record. Or set to current record being viewed.
$array_position = !empty($_POST["key"]) ? $_POST["key"] : 0;

// Check to see if the First button was pressed and set the session array pointer to the first record.
if(!empty($_POST["first"]))
{
 $sw = 0; // This variable set to 0 so the Update button appears.
 $array_position=0;
 }
 
// Check to see if the Next button was pressed.
if(!empty($_POST["next"]))
{
 // Check to see if you are currently viewing a record.
 // If yes, then increment the session array pointer to the next record.
 // But not past the first empty array element.
 if(isset($_SESSION["fname"][$array_position]))
 {
  $array_position++;
  // If the next record is not empty then display the Update button.
  if(!empty($_SESSION["fname"][$array_position])){$sw = 0;}
  else{$sw=1;} // If it is empty, then display the Add button.
  }
 }
 
// Check to see if the Prev button was pressed. If yes, then decrement the session array pointer to the previous record.
if(!empty($_POST["prev"]))
{
 $sw = 0; // Display the Update button.
 $array_position--;
 if($array_position<0){$array_position = 0;} // Don't let the session array pointer get less than 0.
 }

// Check to see if the Last button was pressed.
// If yes, then set the session array pointer to the last record.
if(!empty($_POST["last"]))
{
 $sw = 0; // Display the Update button.
 $array_position=count($_SESSION["id"])-1;
 }
 
// Check to see if the Update button was pressed.
// If yes, then update the record in the database and the session array.
if(!empty($_POST["update"]))
{
 mysql_query("UPDATE testable1 SET lname = '".$_POST['lname']."',fname = '".$_POST['fname']."',address = '".$_POST['address']."',address2 = '".$_POST['address2']."',city = '".$_POST['city']."',state = '".$_POST['state']."',zip = '".$_POST['zip']."',phone = '".$_POST['phone']."',notes = '".$_POST['notes']."' WHERE id = '".$_SESSION["id"][$array_position]."'");
 $_SESSION["lname"][$array_position]=$_POST['lname'];
 $_SESSION["fname"][$array_position]=$_POST['fname'];
 $_SESSION["address"][$array_position]=$_POST['address'];
 $_SESSION["address2"][$array_position]=$_POST['address2'];
 $_SESSION["city"][$array_position]=$_POST['city'];
 $_SESSION["state"][$array_position]=$_POST['state'];
 $_SESSION["zip"][$array_position]=$_POST['zip'];
 $_SESSION["phone"][$array_position]=$_POST['phone'];
 $_SESSION["notes"][$array_position]=$_POST['notes'];
 $_SESSION["meeting"][$array_position]=$_POST['meeting'];
 $sw=0; // Display the Update button.
 }

// Check to see if the Add button was pressed.
// If yes, then add the record to the session array and insert the record into the database.
// By default $sw is previously set to 1 to display the Add button.
if(!empty($_POST["add"]))
{
 $_SESSION["id"][]=$_SESSION["last_id"]++;
 $_SESSION["lname"][]=$_POST['lname'];
 $_SESSION["fname"][]=$_POST['fname'];
 $_SESSION["address"][]=$_POST['address'];
 $_SESSION["address2"][]=$_POST['address2'];
 $_SESSION["city"][]=$_POST['city'];
 $_SESSION["state"][]=$_POST['state'];
 $_SESSION["zip"][]=$_POST['zip'];
 $_SESSION["phone"][]=$_POST['phone'];
 $_SESSION["notes"][]=$_POST['notes'];
 $_SESSION["meeting"][]=$_POST['meeting'];
 $insert = "INSERT INTO testable1(lname,fname,address,address2,city,state,zip,phone,notes, meeting) VALUES('".$_POST['lname']."','".$_POST['fname']."','".$_POST['address']."','".$_POST['address2']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['notes']."','".$_POST['meeting']."')";
 $results = mysql_query($insert) or die ("Couldn't insert into database because: " .mysql_error());


 }
?>

The date is: &nbsp;&nbsp; <b><i><?php echo "$date";?></b></i><br>
The time is:&nbsp;&nbsp; <b><i><?php echo "$time";?></b></i><br>

<br><br>

<form name ="form2" method ="post" action ="#">
  <p>Last Name &nbsp;<input type ="text" name ="lname" value ="<?php echo $_SESSION["lname"][$array_position]; ?>"> &nbsp;&nbsp;First Name<input type ="text" name ="fname" value="<?php echo $_SESSION["fname"][$array_position]; ?>">
     
  <br>
  <br>
    Address&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
  <input type+"text" name ="address" value ="<?php echo $_SESSION["address"][$array_position]; ?>"><br><br>
    Address 2&nbsp;&nbsp;&nbsp;&nbsp;<input type ="text" name ="address2" value ="<?php echo $_SESSION["address2"][$array_position]; ?>"><br><br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;City&nbsp;&nbsp;&nbsp;&nbsp;<input type ="text" name ="city" value ="<?php echo $_SESSION["city"][$array_position]; ?>">&nbsp;&nbsp;
    State<input type ="text" name ="state" size ="2" value ="<?php echo $_SESSION["state"][$array_position]; ?>">&nbsp;&nbsp;
    Zip<input type ="text" name ="zip" value ="<?php echo $_SESSION["zip"][$array_position]; ?>">
  </p>
  <p>Meeting date: <input type ="text" name ="meeting" value ="<?php echo "$date";?>" <br>
    <br>
    
    <?php
    // If $sw is set to 1 then the Add button is displayed.
    if($sw == 1)
    {
    ?>
    <input type="submit" name="add" value="Add">
    <?php
     }
    // If it is set to 0 then the Update button is displayed.
    else
    {
    ?>
    <input type="submit" name="update" value="Update">
    <?php
     }
    ?>
    </td>
  </p>
   <td colspan=2 align="center" style="color:#00f;">
    Record <b>-</b>
    <?php
     // Display the current or new record number.
     echo $pos = $array_position+1;
     if($sw == 0)
     {
      // Display the total number of records if any exist.
      // Unless you are adding a record ($sw will equal 1). Then only display the new record number.
      echo " of ".count($_SESSION["lname"]);
      }
    ?>
  </td>
   <td><input type="submit" name="first" value="First"><input type="submit" name="prev" value="Prev"><input type="submit" name="next" value="Next"><input type="submit" name="last" value="Last"></td>
  </tr>
 </table>
 <input type="hidden" name="key" value="<?php echo $array_position; ?>">
</form>
<?php  
 if(mysql_affected_rows()==1)
  {
  echo 'Data inserted successfully!';
  $sw=0; // A new record has been created so display the Update button.
  }
 ?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-14-09, 02:27 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
If you have SSH access, you can use mysql directly, and then when you have the commands running, put them into PHP.

To get into mysql with SSH - use:

mysql -u[username] [database] -p[password]

For example:

mysql -ubob bobs_database -pbobs_password

Then you can test out the commands directly.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Multi script PayPal add to cart order form Tito1 JavaScript 5 02-23-09 05:21 AM
How do add a template column dynamically? phathutshedzo ASP.NET 2 01-26-07 04:54 AM
Firefox and align problems gigafare CSS 12 01-07-07 12:22 PM
Add column information. Questionare leocordero JavaScript 1 03-03-05 08:35 PM
I most definately suggest DevelopingCentral.com For Any Website Design/Development! Salty777 General Advertisements 2 10-01-04 05:27 AM


All times are GMT -5. The time now is 08:21 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.