Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert blank row and update query

Status
Not open for further replies.

EdLentz

Technical User
Mar 20, 2002
85
US
I am not having any luck getting this to work. As far as I can tell I want to create a CRUD system. I have a table with 6 fields, I need a way to
a. List all the records
b. Provide a way to Insert a new record
C. Edit an existing record.
Here is what I have so far. I know it is messy.
Code:
<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="cqadmin"; // Database name 
$tbl_name="phonebooks"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name order by phonebookname";
$result=mysql_query($sql);

// Count table rows 
$count=mysql_num_rows($result);
?>

<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr> 
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">

<tr>
<td align="center"><strong></strong></td>
<td align="center"><strong>Phonebook</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Office</strong></td>
<td align="center"><strong>Cell</strong></td>
<td align="center"><strong>Other</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td align="center">
<? $id[]=$rows['id']; ?><?php echo $rows['id']; ?>
</td>
<td align="center">
<input name="phonebookname[]" type="text" id="phonebookname" value="<?php echo $rows['phonebookname']; ?>">
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>
<td align="center">
<input name="office[]" type="text" id="office" value="<?php echo $rows['office']; ?>">
</td>
<td align="center">
<input name="cell[]" type="text" id="cell" value="<?php echo $rows['cell']; ?>">
</td>
<td align="center">
<input name="other[]" type="text" id="other" value="<?php echo $rows['other']; ?>">
</td>
</tr>

<?php
}
?>

<tr>
<td colspan="4" align="center"><input type="submit" name="Add" value="Add"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>

<?php

// Check if button name "Add" is active, do this 
if($Submit){
$sql="INSERT INTO phonebooks
    (id,phonebookname,name,office,cell,other)
VALUES
    (?,?,?,?,?,?)";
$result1=mysql_query($sql);
}
if($result1){
header("location:test.php");
}
mysql_close();
?>
</table>

So I get the list and so far I can click on the button but nothing happens. Once I get that working I need to move onto editing. Eventually all this will lead to creating a xml file IF I am still sane enough to finish it! :))
 
THe first thing I would suggest is to turn on error reporting. You have a couple errors that should be pretty evident with it on.

Second thing, is to stop using the mysql functions as they are deprecated and will stop being supported in future versions of PHP. use mysqli instead. Works the same, but will have support going forward.

Third thing I would suggest, is if you are going to rely on register_globals being turned on, and as such form inputs automatically being turned into variables make sure the variable names you are trying to use are correct. Your Form has an input named "Add", but you are checking for a variable named $Submit which is not set anywhere. If you have register_globals turned on it would be $Add instead. However I strongly suggest you get in the habit of not using register_globals, and instead manually retrieving the form values from $_POST or $_GET variables depending on your form's submit method.

Fourth thing, you may want to check for errors in your queries using the mysql(i)_error() function. I don't believe having raw ? is valid in a mysql statement.

Code:
$result1=mysql_query($sql) [b]or die (mysql_error())[/b];



----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top