INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Insert blank row and update query

Insert blank row and update query

(OP)
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! :))

RE: Insert blank row and update query

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) or die (mysql_error()); 

----------------------------------
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close