Page 1 of 1

mysql_query & INSERT INTO

Posted: Sat Jul 21, 2012 3:45 am
by Ghoz
Hi, I've tried to modify the script below but to no avail! The script below has been edited for general view.
I have a table 'users' with columns 'Id' and 'Name'
I have another table 'pra' with columns 'User_Id', 'Pra_Id' & 'Name'

When the script below is runs, it suppose to query the column 'Name' from the table 'users' and insert it into the column 'Name' of table 'pra'. Other data will also be inserted into 'User_Id', 'Pra_Id'.
Please help. Thanks!

Code: Select all

<?php 
$visitingfiledirectly = false;
if($visitingfiledirectly)
{
	?>
    <link rel="stylesheet" type="text/css" media="screen" href="../stylus.css">
    <?php
}
?>
<?php
$id = 0;
$b = 0;
$user_pra_id = -1;

$printpra = true;

if(isset($_GET["id"]) && is_numeric($_GET["id"]))
{
	$id = $_GET["id"];
}
if(isset($_GET["b"]) && is_numeric($_GET["b"]))
{
	$b = $_GET["b"];
}
if(isset($_GET["id"]) && is_numeric($_GET["id"]) && isset($_GET["delete"]) && is_numeric($_GET["delete"]) && isset($_SESSION["username"]) && isset($_SESSION["password"]))
{
	$admin = getMyAdminLevel();
	$checkpra = mysql_query("SELECT Owner FROM pra WHERE Id='".$id."'"); // FIND IF THERE IS A VALIDATION ENTRY WITH THE CODE RECEIVED
$pra = mysql_fetch_array($checkpra);

	if(($admin >= 1 || $pra["Owner"] == $_SESSION["username"]) && $_GET["delete"] == 7)
	{
		mysql_query("DELETE FROM pra WHERE Id='".$_GET["id"]."'") 
		or die(mysql_error());  	
		?><div class="warning">blablabla!</div><?php
		$printpra = false;
	}
}
if($printpra)
{
?>
<table border="0" width="100%">
<?php 
$checkpra = mysql_query("SELECT Id, Owner, Title, Short, Description, Date FROM pra WHERE Id='".$id."'"); // FIND IF THERE IS A VALIDATION ENTRY WITH THE CODE RECEIVED
$pra = mysql_fetch_array($checkpra);
$checkuser = mysql_query("SELECT Id, Avatar FROM users WHERE Username='".$pra["Owner"]."'"); // FIND IF THERE IS A VALIDATION ENTRY WITH THE CODE RECEIVED
$user = mysql_fetch_array($checkuser);
$user_pra_id = $user["Id"];
$admin = getMyAdminLevel();

//------------------------
$result = mysql_query("SELECT Name from users WHERE Username='".$_SESSION["username"]."'");
$row = mysql_fetch_array($result);
		$name = $row["Name"];

		$query = 'INSERT INTO pra (User_Id, Pra_Id, Name)
        VALUES (\''.$user_pra_id.'\',\''.$pra_id.'\',\''.$name.'\')';
		mysql_query($query) or die(mysql_error());

if($pra > 0)
{
?><div class="praentry"><?php
if($admin >= 1 || (isset($_SESSION["username"]) && $_SESSION["username"] == $pra["Owner"]))
{
	?>
		<div class="admin">
		<div class="previous"><a href="index.php?section=editpra&id=<?php echo $_GET["id"];?>"><b>Edit this petition</b></a></div>
		<div class="next"><a href="index.php?section=readpra&id=<?php echo $_GET["id"];?>&delete=7"><font color="#FF0000"><b>Delete this</b></font></a></div>
		</div>
   <?php
}
if($b == 1)
{
	setPra($id);
}
?>
<tr><td><div class="miniavatar">
<?php useLightbox($user["Avatar"],$pra["Owner"],50,50);?>
</div>
<div class="pramini">
<h1><a href="index.php?section=readpra&id=<?php echo $pra["Id"];?>"><?php echo $pra["Title"];?></a></h1><br>
<?php echo $pra["Short"];?><br>
submitted by <a href="index.php?section=profile&uid=<?php echo $user["Id"];?>"><?php echo $pra["Owner"];?></a> <?php echo referencialHour($pra["Date"]);?>
</div>
</div></td></tr>
<tr><td><div class="pradescription">
<?php echo $pra["Description"];?><br>
<div style="text-align:right; width:100%; border:0px;">submitted by <a href="index.php?section=profile&uid=<?php echo $user["Id"];?>"><?php echo $pra["Owner"];?></a> <?php echo referencialHour($pra["Date"]);?></div>
</div></td></tr>
<?php
if($admin >= 0)
{
	?>
	<tr><td><?php
	$pra = ifPra($id);
	if(($pra == -1 || $pra == 0) && $pra["Owner"] != $_SESSION["username"])
	{
?>
<div class="pfor">
You haven't.<br>
please <a href="index.php?section=readpra&id=<?php echo $id;?>&b=1">click here</a>
<?php
if($admin >= 1)
{
?><br>People who has:<br><?php
printWhoHasPra($id);
}
?></div><?php
	}
	if($pra == 1  && $pra["Owner"] != $_SESSION["username"])
	{
?><div class="pra"><font color="#4CC417"><b>You have:</b><br></font><img src="ic/state1.png"><br>
People who has:<br>
<?php
printWhoHasPra($id);
?>
</div>	
<?php
	}
if($pra["Owner"] == $_SESSION["username"])
	{
?><div class="pra">
Who has:<br>
<?php
printWhoHasPra($id);
?></div><?php	
	}
	?></td></tr><?php
}
?>
</table>
<?php 
if(isset($_SESSION["username"]) && isset($_SESSION["password"]))
{
	$myid = getUserId($_SESSION["username"]);
	if($myid != $user_pra_id  && isNotAdminId($user_pra_id))
	{
include_once('SECTIONS/reportbutton.php'); // SET THE USER LOGIN AT THE LEFT
	}
}
}
else
{
	?><div class="warning">Whoops! You must have arrived here by error!<br>.</div>
	<meta HTTP-EQUIV="REFRESH" content="0; url=index.php"><?php
}
}
?>

Re: mysql_query & INSERT INTO

Posted: Sat Jul 21, 2012 3:50 am
by social_experiment
Ghoz wrote:it suppose to query the column 'Name' from the table 'users' and insert it into the column 'Name' of table 'pra'.
what currently happens to the script when you execute it?

Re: mysql_query & INSERT INTO

Posted: Sat Jul 21, 2012 8:04 am
by mikosiko
Ghoz wrote:
it suppose to query the column 'Name' from the table 'users' and insert it into the column 'Name' of table 'pra'.
first and foremost what you are trying to do is incorrect!!

according to the information that you have provided you have:
a table USER with fields:
- id
- name

and a table PRA with at least this fields:
- user_id
- pra_id

and in this table you want to insert the name that you already have in the table USER right?... why?... if you already have the name in the user table then you can recover that name anytime using a simple JOIN... what you are trying to do break a basic rule of normalization in a relational database and must be avoided.

Re: mysql_query & INSERT INTO

Posted: Sat Jul 21, 2012 9:31 am
by Ghoz
When executed this is the result (3 rows):

User_Id Pra_Id Name
30 185
29 0 Sabrin
29 0 Sabrin

The correct result should be (1 row):
User_Id Pra_Id Name
30 185 Sabrin

I need to insert the 'name' into different table ' users' & table 'pra' because these 2 tables contain different data. Simply to say I need to insert it into different tables & easier for me to access it later. Thanks.