Jump to content

Using variable from 2 table via php


bear24

Recommended Posts

Hi,

 

I am trying to delete some of the post from my forum. But there is an error. Do help me!

 

The followingis my code

<?php include('Indexheader.php') ?>
<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="root"; // Mysql password 
$db_name="advert"; // Database name 
$tbl_name="forum_question"; // 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 address bar 
$id=$_GET['id'];

$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<?php
$tbl_name2="forum_answer"; // Switch to table "forum_answer" 

$sql2="SELECT a_id, a_name, a_email, a_answer, a_datetime, question_id FROM $tbl_name2 WHERE question_id='$id'";
$result2=mysql_query($sql2);

while($rows=mysql_fetch_array($result2)){
?>


<table width="555" border="0" cellspacing="1" cellpadding="0" align="center">
<tr>
<td><form name="udform" method="GET" action="udel.php"  enctype="multipart/form-data">
	<table width="550" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
	<tr>
	<td width="41" bgcolor="#FFFFFF"> </td>
	<td colspan="7" bgcolor="#FFFFFF"><strong>Existing Data in table as follows:</strong> </td>
	</tr>

	<tr>
	<td align="center" bgcolor="#FFFFFF"><strong>No.</strong></td>
	<td width="37" align="center" bgcolor="#FFFFFF">#</td>
	<td width="110" align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
	<td width="114" align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
	<td width="128" align="center" bgcolor="#FFFFFF"><strong>Answer</strong></td>
	<td width="77" align="center" bgcolor="#FFFFFF"><strong>Date/time</strong></td>
	</tr>
		<?php $j=1; while($j<=$count) { ?>		<!-- for generating serial no for dynamic column sl - open code-->
		<?php while($rows=mysql_fetch_array($result2, MYSQL_ASSOC)){ ?>
	<tr>
	<td bgcolor="#FFFFFF"> <?php echo $j; ?></td>
	<td align="center" bgcolor="#FFFFFF"><input name="del_id[]" type="checkbox" id="del_id[]" value="<? echo $rows['a_id']; ?>"></td>
	<td bgcolor="#FFFFFF"><? echo $rows['a_name']; ?></td>
	<td bgcolor="#FFFFFF"><? echo $rows['a_email']; ?></td>
	<td bgcolor="#FFFFFF"><? echo $rows['a_answer']; ?></td>
	<td bgcolor="#FFFFFF"><? echo $rows['a_datetime']; ?></td>
	</tr>
		<?php  $j++; } ?> <!-- for generating serial no for dynamic column sl - close code-->
		<?php } ?>
	<tr>
	<td colspan="8" align="center" bgcolor="#FFFFFF"><input name="del" id="del" type="submit" value="DELETE"></td>
	</tr>
	</table>
</form>
</td>
</tr>
</table>
<?php include('Indexfooter.php') ?>  

 

Please assist and thanks a lot!

 

bear

Link to comment
https://forums.phpfreaks.com/topic/125581-using-variable-from-2-table-via-php/
Share on other sites

Hi Bear,

 

You don't seem to have any SQL query that might indicate that you are trying to delete something?

 

Also, change this

 

// get value of id that sent from address bar 
$id=$_GET['id'];

 

to this:

 

// get value of id that sent from address bar 
$id=mysql_real_escape_string($_GET['id']);

 

in order to prevent SQL injection.

In fact i am trying to merge part of 2 code together.

 

the two codes are:

<?php include('Indexheader.php') ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="./tbl1.css" rel="stylesheet" type="text/css" media="all">
<title>Users List</title>
<style type="text/css" media="all">

body		{
			margin-top: 25px;
			margin-left: 0px;
			background-color:#999999;
			font-family:Geneva, Arial, Helvetica, sans-serif;
			font-size:14px;
			}
td			{
			background-color:#99CC99;
			vertical-align:top;
			}
</style>
</head>
<body>

<?php 

require("./db_connect.php");
$sql="SELECT id, topic, detail, view, reply, datetime FROM forum_question order by id ASC";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

?>


<table width="555" border="0" cellspacing="1" cellpadding="0" align="center">
<tr>
<td><form name="udform" method="GET" action="udel.php"  enctype="multipart/form-data">
	<table width="550" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
	<tr>
	<td width="37" bgcolor="#FFFFFF"> </td>
	<td colspan="7" bgcolor="#FFFFFF"><strong>Existing Data in table as follows:</strong> </td>
	</tr>

	<tr>
	<td align="center" bgcolor="#FFFFFF"><strong>No.</strong></td>
	<td width="34" align="center" bgcolor="#FFFFFF">#</td>
	<td width="101" align="center" bgcolor="#FFFFFF"><strong>Topic</strong></td>
	<td width="171" align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
	<td width="41" align="center" bgcolor="#FFFFFF"><strong>Views</strong></td>
	<td width="48" align="center" bgcolor="#FFFFFF"><strong>Replies</strong></td>
	<td width="68" align="center" bgcolor="#FFFFFF"><strong>Date/time</strong></td>
	</tr>
		<?php $j=1; while($j<=$count) { ?>		<!-- for generating serial no for dynamic column sl - open code-->
		<?php while($rows=mysql_fetch_array($result, MYSQL_ASSOC)){ ?>
	<tr>
	<td bgcolor="#FFFFFF"> <?php echo $j; ?></td>
	<td align="center" bgcolor="#FFFFFF"><input name="del_id[]" type="checkbox" id="del_id[]" value="<? echo $rows['id']; ?>"></td>
	<td bgcolor="#FFFFFF"><a href="user_dlist_answer.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a></td>
	<td bgcolor="#FFFFFF"><? echo $rows['detail']; ?></td>
	<td bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
	<td bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
	<td bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
	</tr>
		<?php  $j++; } ?> <!-- for generating serial no for dynamic column sl - close code-->
		<?php } ?>
	<tr>
	<td colspan="8" align="center" bgcolor="#FFFFFF"><input name="del" id="del" type="submit" value="DELETE"></td>
	</tr>
	</table>
</form>
</td>
</tr>
</table>
<?php include('Indexfooter.php') ?>  
</body>
</html>

 

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="root"; // Mysql password 
$db_name="advert"; // Database name 
$tbl_name="forum_answer"; // 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 answer number. 
$sql="SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1 
if ($rows) {
$Max_id = $rows['Maxa_id']+1;
}
else {
$Max_id = 1;
}

// get values that sent from form 
$a_name=$_POST['a_name'];
$a_email=$_POST['a_email'];
$a_answer=$_POST['a_answer']; 

$datetime=date("d/m/y H:i:s"); // create date and time 

// Insert answer 
$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";
$result2=mysql_query($sql2);

if($result2){
echo "Successful<BR>";
echo "<a href='view_topic.php?id=".$id."'>View your answer</a>";

// If added new answer, add value +1 in reply column 
$tbl_name2="forum_question";
$sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE id='$id'";
$result3=mysql_query($sql3);

}
else {
echo "ERROR";
}

mysql_close();
?>

 

 

The aim is to delete the specify post from each thread.

But i dont know how to do it. I try all ways to merge but ifdont seems to work.

 

bear

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.