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: <b><i><?php echo "$date";?></b></i><br>
The time is: <b><i><?php echo "$time";?></b></i><br>
<br><br>
<form name ="form2" method ="post" action ="#">
<p>Last Name <input type ="text" name ="lname" value ="<?php echo $_SESSION["lname"][$array_position]; ?>"> First Name<input type ="text" name ="fname" value="<?php echo $_SESSION["fname"][$array_position]; ?>">
<br>
<br>
Address
<input type+"text" name ="address" value ="<?php echo $_SESSION["address"][$array_position]; ?>"><br><br>
Address 2 <input type ="text" name ="address2" value ="<?php echo $_SESSION["address2"][$array_position]; ?>"><br><br>
City <input type ="text" name ="city" value ="<?php echo $_SESSION["city"][$array_position]; ?>">
State<input type ="text" name ="state" size ="2" value ="<?php echo $_SESSION["state"][$array_position]; ?>">
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.
}
?>