2 Replies - 2227 Views - Last Post: 26 January 2011 - 10:13 AM Rate Topic: -----

#1 tbzserge  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 26-January 11

Help with PHP/MySQL database Insert and Delete Problem

Posted 26 January 2011 - 09:55 AM

>.< Hello,everyone.I am a begineer and have just learnt using PHP language and MySQL database.I have encountered problem with insert and delete function.The problem is when i insert,it cannot search the topic in database with the highest no.id and +1 for the following new topic that inserted.The new topic that i insert to the database is always with the id no.1 Then,about the delete function,when i try to delete,i cannot delete it from database and i can't find where's my mistake is.

latestEvent.php
  
 <div id="news"> 
     		<h1>Latest Event</h1><p>
		<span class="forum_locheader"></span><br />
		<a href="index.php">Home</a>
<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="fyp"; // Database name 
$tbl_name="event"; // 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 id ASC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>

  
	</p><table border="1" cellpadding="0" cellspacing="0" width="150%">
	<br/>
		<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<form>
		<tr>
		<?php echo $rows['topic']; ?><br/><?php echo $rows['detail']; ?><a href="delete_topic2.php"><img src="images/delete.jpg" border="0" /></a><br/>

		  
      <p class="comments align-right"> <a href="forum1.php">Read 
	  more in forum section</a> </p> <a href="updateEvent.php">Update</a>
<br/>
<br/>
<br/>		  

		 
		</tr>
</form>	

	
		<?php
// Exit looping and close connection 
}
mysql_close();
?>
<div class="newsitems"><p class="comments align-left">More news about latest event 
	 in  <a href="forum1.php"> FORUM</a> <a href="forum1.php"><img src="images/arrowhead.gif" width="100px" height="50px" border=""/></a></p>
      <br/>
      </div>
    <div class="clear"></div>
    

<tr>
<td colspan="1" align="right" bgcolor="white"><a href="create_topic1.php"><strong>
Add a new Event</strong> </a></td>
</tr>

</table>

</div>



create_topic1.php
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Create New Topic</title>
<style type="text/css">
.style1 {
	margin-bottom: 0px;
}
.style2 {
	margin-left: 329px;
}
</style>
</head>

<body>

<table width="700" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="white">
<tr>
<form id="form1" name="form1" method="post" action="add_topic4.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td colspan="3" bgcolor="#E6E6E6"><strong>Add New Event</strong> </td>
</tr>
<tr>
<td style="width: 21%"><strong>Topic</strong></td>
<td width="2%">:</td>
<td width="84%"><input name="topic" type="text" id="topic" size="50" /></td>
</tr>
<tr>
<td valign="top" style="width: 11%"><strong>Detail</strong></td>
<td valign="top">:</td>
<td>
<textarea name="detail" id="detail" class="style1" style="width: 474px; height: 181px"></textarea></td>
</tr>
<tr>
<td style="width: 11%"><strong>Name</strong></td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50" style="width: 357px" /></td>
</tr>
<!--<tr>
<td style="width: 11%"><strong>Email</strong></td>
<td>:</td>
<td><input name="email" type="text" id="email" size="50" style="width: 358px" /></td>
</tr>-->
<tr>
<td style="width: 11%">&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<form>
	<input type="button" value="Back" onclick="history.go(-1); return true;" style=" width: 118px" class="style2"/></form>


</body>

</html>



this is the code for the insert function: add_topic.php
<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="fyp"; // Database name 
$tbl_name="event"; // 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");

// Get value of id that sent from hidden field 
$id=$_POST['id'];

// Find highest number. 
$sql="SELECT MAX(id) AS Maxa_id FROM $tbl_name WHERE topic='$topic'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

// add + 1 to highestnumber and keep it in variable name "$Max_id". 
if ($rows) {
$Max_id = $rows['Maxa_id']+1;
}
else {
$Max_id = 1;
}

// get values that sent from form 
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name']; 

$sql2="INSERT INTO $tbl_name(id,topic,detail,name)VALUES('$Max_id', '$topic', '$detail', '$name')";
$result2=mysql_query($sql2);

if($result2){
echo "Successful<BR>";
echo "<a href='latestEvent.php".$id."'>View event</a>";



}
else {
echo "ERROR";
}

mysql_close();
?>




This is my code for delete function: delete_topic.php
<?php

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

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

//get data sent from form;
$id = $_GET['id'];
echo $id;

$delete1="DELETE FROM event WHERE id='$id'";
$result4=mysql_query($delete1);

if($result4)
{
echo "Successful<BR/>";
echo "<a href=latestEvent.php>View the page</a>";
}
else 
{
echo "ERROR";
}

//}
mysql_close();
?>
<a href="latestEvent.php >View your event"</a>



my MySQL database query:
Table event
CREATE TABLE `event`
`id` int(5) NOT NULL auto_increment,
`topic` varchar(255) NOT NULL default '',
`detail` longtext NOT NULL,
`name` varchar(65) NOT NULL default '',

PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;


Any help i would be really appreciate.Thanks >.<

Is This A Good Question/Topic? 0
  • +

Replies To: Help with PHP/MySQL database Insert and Delete Problem

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10492
  • View blog
  • Posts: 38,876
  • Joined: 27-December 08

Re: Help with PHP/MySQL database Insert and Delete Problem

Posted 26 January 2011 - 10:09 AM

The CREATE TABLE query has parentheses around the column names. Also, good practice to define the PK to be UNIQUE.
CREATE TABLE table_name(
     table_name_id int NOT NULL UNIQUE PRIMARY KYEY AUTO_INCREMENT,
     field2 type_2,      
);


Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2960
  • View blog
  • Posts: 10,186
  • Joined: 08-August 08

Re: Help with PHP/MySQL database Insert and Delete Problem

Posted 26 January 2011 - 10:13 AM

You can't find your mistake because your code is spaghetti. Separate the languages. You have PHP, CSS and HTML in the same files. That's a recipe for confusion.

Do NOT use tables for formatting.

Use proper indenting.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1