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

Recommended Answers

All 40 Replies

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

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

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

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

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

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

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

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





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


?>
</body>
</html>

and thnx for help

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

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

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 "<br><br>";
$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><br><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

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

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 "<br><br>";
$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><br><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

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

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 ?

this code does not work at all:S

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

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

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

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

i can post for u all the code ....but i can't import the DB ..but i can tell u what my tables have .....

i will try what shawncplus typed .........and thnx alot both of u for helping me

What shawncplus typed is correct. If you look at the code I gave you that is exactly what i'm doing however there seems to be something going wrong to create all the errors. It'l probably be something very small.

Like i said, attach all the files and il do what you like.

Thanks

index.php

<html>
<head>
<title>User Logon</title>
  <h2>User Login </h2>
  <body bgcolor=grey >

  <b><a href="index.php">Abdullah's Forum</a><b><br><br>

  <form name="login" method="post" action="login1.php">
   Username: <input type="text" name="un"><br>
   Password:<input type="password" name="pass"><br>
   Remember Me: <input type="checkbox" name="rememberme" value="1"><br>
   <input type="submit" name="submit" value="Login!">
  </form>
<a href="signup.html">Signup</a> <br><br><br>
<?php
$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><br><hr>';


       }


?>

</body>

</html>

Is there any difference in those or did you post three copies of the exact same thing?

Yeh they look all the same?
Go to 'reply to thread' (NOT quick reply).
And if you scroll down you can attach files.
Attach all the associated files to the post and then describe to me the mysql table you have.
Thanks

sorry .........my browser got crazy a bit ......:S:S

It's ok. I'll clean out the first two. I didn't want to delete them if there actually was a slight edit in the code that you intended.

So, can you post the entire system ?

there are all my scripts
i changed the name of welcome.php to home.php
home1.php is for the admin
delete.php does not give me any error ....but it dosen't give a result
my DB contain the Tables :
comments
topics
users
------------------------
comment:
id
title
message
author
topicid
--------------------------
topics
id
subject
author
description
tdate ----->this is the topic date but i don't know how to show current date
----------------------------
users
id
name ----->i mean with it username
pass
firstname
lastname
email
phone
adress
city
country
posts
loggedin
---------------------------------------------------
that's everything i have ........thank u alot in advance for helping me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.