954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

insert data where

hi all ..i'm working on php application .......and i need to insert data into a filed in my table but in a certain condition ......exp:(insert into users (posts) values(...) where ...) i can't do this .....what should i do ? please help and thnx in advance

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

There's no such thing as a conditional insert, it just appends to the end of the table. Do your conditional in PHP.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

It depends if your comparing something in the database or something logicall for example if 1=1 or if a username is equal to a certain field.

Post some more code/details and il try help, but if its something non associated like comparing two variables it should (like above said) be done in PHP.

Hope this helps

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 

i want to insert data into my DB in a certain row ...can i do it?

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

Hi and happy Easter :)

Unfortunately, its impossible as far as I know to do this.
You should therefore do you condition in PHP and then insert it to the database.
When you say a certain row. Do you mean checking a variable against a value in the database?
If you post up some details il be happy to help with the code.

Hope this helps

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 

thnx for help ..the thing i'm trying to do ..is that i want to make a login system ...when the user make the login i want to change a variable in the DB to 1 and when he goes offline i want to make this row or variable 0 ...i want to this to be able to show all the logged in user on my site ...hope u understand what i want..and i have another question ..how can i add the result of 2 count query from different tables ?..and thnx alot in advance

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

Hi

Could you post the login code you already have and il intigrate what you like. Its hard for me to presume what you already have.

Thanks

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 
<?php

    session_start();


   ?>
<html>

<head>
  <title></title>
</head>

<body>

<?php

$dbh=@mysql_connect('localhost','root','');
if(!$dbh){
exit('Unable to connect');
}

if(!@mysql_select_db('ijdb')){
exit('Unable to find ijdb');
}
$result=@mysql_query('select * from users');
if(!$result){
exit('no data found');
}
$t=0;
$u=$_REQUEST['un'];
$p=$_REQUEST['pass'];
$encpass=md5($p);
while($row=mysql_fetch_array($result)){
	if(($row['name']== $u) and ($row['pass']==$encpass)){
		$t=1;
	}
}


if($t==1){

$_SESSION['user']=$u;
$_SESSION['pass']=$p;

echo '<meta http-equiv="Refresh" content="2; url=http://www.forum.com/welcome.php" />';

  print "<center>  Thank you for logging in, $u 

Click here if your browser does not automatically redirect you.</center>";





}
if($t==0){
	echo FAUX;
}


?>
</body>
</html>

and thnx for help

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

that's my login code ..i want when a user log in .......that his name be regsitred in the page redirected from this page (welcome.php) ..and all the logged in users in the same place...and thnx in advance

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

The way I see it you should add the code to your welcome.php:
Perhaps

if (!isset ($_SESSION['loggedin'])) {
header('Location: http://www.yoursite.com/pleaselogin.php');
} 
else {
$username = $_SESSION['un'];
$password = $_SESSION['pass'];

$query = "UPDATE table_members SET loggedin=1 WHERE username='$username' OR password='$password'  ";
if (mysql_query($query)) {
?>
INCLUDE YOUR PAGE CONTENT
<?php
} else {
echo "Error, Couldnt Update";
}
}


For this to work you need to add

$_SESSION['loggedin'] = true


just before the user is redirected on the previous page. Also remember to change the names for example the table name.
Lastly bear in mind i'm tired so if there are any errors post them up and il write you a perfect system in the morning. Sorry if there are.

Im off to bed, hope this helps

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 

i will post for you the welcome.php code ...

<?php

session_start();


?>
<html>
      <meta http-equiv="Refresh" content="300; url=http://www.forum.com/logout.php" />
<head>
  <title></title>
</head>

<body>

<?php



$username=$_SESSION['user'];

if(isset($_SESSION['user'])){

print "Welcome &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $username ";
echo "";
$dbh=@mysql_connect("localhost", "root", "")
       or die ("Could not connect to MySQL");

     $db=mysql_select_db ("ijdb")
       or die ("Could not select database");



$query = "SELECT * FROM topics";
       $result = mysql_query ($query)
         or die ("Query failed");

       while ($row = mysql_fetch_array($result)) {
       echo '<a href="detailtopic.php?idt=';
       echo  $row['id'];
       echo  '">'.$row['subject'].'</a><hr>';
       }
       echo '<a href="newtopic.php">Add New Topic</a>';

       }
  else{
       	header("Location: http://www.forum.com/logout.php");
  }

?>

</body>

</html>

can u please implement the right code here ........and thnx in advance

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

should i add a field in the users tabel named as(loggedin)?
and thnx for help in advance.

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

Hi, I think this will work as your welcome.php code:

<?php
session_start();
if (!isset ($_SESSION['loggedin'])) {
header('Location: http://www.yoursite.com/pleaselogin.php');
} 
else {
$username = $_SESSION['un'];
$password = $_SESSION['pass'];

$query = "UPDATE table_members SET loggedin=1 WHERE username='$username' OR password='$password'  ";
if (mysql_query($query)) {
?>

<meta http-equiv="Refresh" content="300; url=http://www.forum.com/logout.php" />
<head>
  <title></title>
</head>

<body>

<?php
$username=$_SESSION['user'];

if(isset($_SESSION['user'])){

print "Welcome &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $username ";
echo "";
$dbh=@mysql_connect("localhost", "root", "")
       or die ("Could not connect to MySQL");

     $db=mysql_select_db ("ijdb")
       or die ("Could not select database");



$query = "SELECT * FROM topics";
       $result = mysql_query ($query)
         or die ("Query failed");

       while ($row = mysql_fetch_array($result)) {
       echo '<a href="detailtopic.php?idt=';
       echo  $row['id'];
       echo  '">'.$row['subject'].'</a><hr>';
       }
       echo '<a href="newtopic.php">Add New Topic</a>';

       }
  else{
       	header("Location: http://www.forum.com/logout.php");
  }

} else {
echo "Error, Couldnt Update";
}
}

?>
</body>

</html>


Your logout.php should be as follows:

<?php
if (session_destroy()) {
$username = $_SESSION['un'];
$password = $_SESSION['pass'];
$query = "UPDATE table_members SET loggedin=0 WHERE username='$username' OR password='$password'  ";
if (mysql_query($query)) {
echo "Logged Out, And Status Updated";
}
} else {
echo "Couldnt Logout!";
}
?>


Hope this helps and if anythings wrong post back and il have a look

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 

should id add a filed named (logged in) to my users table?
and the $_SESSION['loggedin'] is not set form the beginning ....where should i set it?

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

In the previous code you test if the username and password is in the script, you then redirected the user to welcome.php.
Place

$_SESSION['loggedin']


Just before the redirect.

All you need to do is create a new colum in your table and call it loggedin. It should be set as an int.

Is that what you were asking ?

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 

this code does not work at all:S

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

it has 1 billion error ...........and does not give any result ......the page is immediatly redirected to logout.php ........
plz help me with a workable code :S

weblover
Junior Poster
141 posts since Feb 2009
Reputation Points: 10
Solved Threads: 1
 

Can you post attach (scroll a little down after clicking "reply to thread" all the relevent pages to your code. Also can you go to your mysql database and export the sql.

If its not possible to do any of that can you post up the errors your getting?
Thanks

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 

If you're trying to modify an already existing row take a look at
UPDATE which can take a WHERE clause
http://dev.mysql.com/doc/refman/5.0/en/update.html

There is also REPLACE. It acts like INSERT but if a row exists with the same primary key, it will replace it.

IE.,

INSERT INTO sometable (id, name) VALUES (0, 'Shawn');

Will give you the table

ID         Name
0          Shawn
REPLACE INTO someTable (id, name) VALUES (0, 'Tester');

You now have

ID         Name
0          Tester


http://dev.mysql.com/doc/refman/5.0/en/replace.html

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Thats what i'm trying to do but the code seems to have not worked in weblover's system. When he comes back online and posts the code il make sure I get it working.

Thanks

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You