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
ShawnCplus 456 Code Monkey Team Colleague
There's no such thing as a conditional insert, it just appends to the end of the table. Do your conditional in PHP.
Designer_101 -2 Posting Whiz
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
weblover 0 Junior Poster
i want to insert data into my DB in a certain row ...can i do it?
Designer_101 -2 Posting Whiz
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
weblover 0 Junior Poster
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
Designer_101 -2 Posting Whiz
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
weblover 0 Junior Poster
<?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
weblover 0 Junior Poster
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
Designer_101 -2 Posting Whiz
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
weblover 0 Junior Poster
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 $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
weblover 0 Junior Poster
should i add a field in the users tabel named as(loggedin)?
and thnx for help in advance.
Designer_101 -2 Posting Whiz
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 $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
weblover 0 Junior Poster
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?
Designer_101 -2 Posting Whiz
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 ?
weblover 0 Junior Poster
this code does not work at all:S
weblover 0 Junior Poster
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
Designer_101 -2 Posting Whiz
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
ShawnCplus 456 Code Monkey Team Colleague
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
Designer_101 -2 Posting Whiz
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
weblover 0 Junior Poster
i can post for u all the code ....but i can't import the DB ..but i can tell u what my tables have .....
weblover 0 Junior Poster
i will try what shawncplus typed .........and thnx alot both of u for helping me
Designer_101 -2 Posting Whiz
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
weblover 0 Junior Poster
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>
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
Is there any difference in those or did you post three copies of the exact same thing?
Designer_101 -2 Posting Whiz
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
weblover 0 Junior Poster
sorry .........my browser got crazy a bit ......:S:S
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster
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.
Designer_101 -2 Posting Whiz
So, can you post the entire system ?
weblover 0 Junior Poster
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
<?php
session_start();
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
$sub=$_REQUEST['sub'];
$desc=$_REQUEST['desc'];
$dbh=@mysql_connect('localhost','root','')
or die('Unable to connect');
$db=@mysql_select_db('ijdb')
or die('DB not found');
if (isset($_POST['user'])) {
$uid = $_POST['un'];
} else {
$user = $_SESSION['user'];
}
$sql="insert into topics (subject,author,description,tdate) values ('$sub','$user','$desc','2-2-2002'))";
$res=@mysql_query($sql)
or die("Unable to execute query");
$insertid=mysql_insert_id();
header("Location: http://www.forum.com/detailtopic.php?idt=$insertid");
?>
</body>
</html>
<?php
session_start();
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
$user=$_SESSION['user'];
$idt=$_REQUEST['idt'];
$title=$_REQUEST['title'];
$msg=$_REQUEST['msg'];
$dbh=@mysql_connect('localhost','root','')
or die ('Cannot Connect');
$db=@mysql_select_db('ijdb')
or die ('Cannot find DB');
$sql="insert into comments (title,message,author,topicid) values ('$title','$msg','$user','$idt')";
$res=mysql_query($sql)
or die('<p>Error creating joke table: ' .
mysql_error() . '</p>');
header("Location: http://www.forum.com/detailtopic.php?idt=$idt");
?>
</body>
</html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$id=$_REQUEST['topics'];
$dbh=@mysql_connect('localhost','root','')
or die('Could not connect');
$db=@mysql_select_db('ijdb')
or die('DB not found');
//$query="delete from topics where id='$todelete'";
$query ="DELETE FROM users WHERE id =$id";
print $query;
$result = mysql_query($query)
or die ('Query Failed');
echo 'Topic Deleted Succesfully';
//echo '<meta http-equiv="Refresh" content="2; url=http://www.forum.com/home1.php" />';
?>
</body>
</html>
<?php
session_start();
?>
<html>
<head>
<title></title>
</head>
<body bgcolor=grey>
<?php
$idt=$_REQUEST['idt'];
$user=$_SESSION['user'];
$dbh=@mysql_connect('localhost','root','')
or die ("Could not connect to MySQL");
$db=mysql_select_db ("ijdb")
or die ("Could not select database");
$result=@mysql_query('select * from topics');
if(!$result){
exit('no data found');
}
while ($row = mysql_fetch_array($result)) {
if($row['id']== $idt){
$u=$row['author'];
$sql="select count(*) from topics where author='$u'";
$res=@mysql_query($sql);
if(!$res){
exit('no data found1');
}
echo ' <b><a href=index.php>Abdullah Forum</a> > <a href=home.php>Home</a> > <a href=detailtopic.php>'.$row['subject'].'</a><b><br><br>';
echo '<b>Subject :'.$row['subject']."
Author: ".$row['author']."
Posts :".$res."<br>";
echo 'Description</b> :<br><br>'.$row['description'];
echo '<hr>';
}
}
$query = "SELECT * FROM comments";
$result = mysql_query ($query)
or die ("Query failed");
while ($row = mysql_fetch_array($result)) {
if($row['topicid'] == $idt){
$title=$row['title'];
$author=$row['author'];
$msg=$row['message'];
print "<b>Title : $title
Author :$author</b> <br> $msg <hr>";
}
}
print "<a href=new_reply.php?idt=$idt>Reply To Thread</a>";
?>
</body>
</html>
<?php
session_start();
?>
<html>
<b><a href="index.php">Abdullah's Forum</a> > <a href="home.php">Home</a> </b><br>
<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 "<table width=100% height=10% border=1 bordercolor=black>
<tr><td>Welcome $username</td>
<td><a href=logout.php>Logout</a></td></tr></table> ";
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>';
$query1= "SELECT * FROM users";
$res = mysql_query ($query1)
or die ("Query failed");
while ($r = mysql_fetch_array($res)) {
echo $r['loggedin'];
}
}
else{
header("Location: http://www.forum.com/logout.php");
}
?>
</body>
</html>
<?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 "<table width=100% height=10% border=1 bordercolor=black>
<tr><td>Welcome $username</td>
<td><a href=logout.php>Logout</a></td></tr></table> ";
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)) {
print "<form action='delete.php' method='post'>";
print '<input type="checkbox" name="topics" value=';
echo $row['id'];
echo '">';
print '<a href="detailtopic.php?idt=';
echo $row['id'];
echo '">'.$row['subject'].'</a>';
print ' <a href=update.php?idt=';
print $row['id'];
print '>Update Topic</a><br><hr>';
//print "<a href=www.forum.com/update.php>Update</a> <a href=www.forum.com/update.php>delete</a>";
}
print "<input type='submit' value='Delete'></form>" ;
echo '<a href="newtopic.php">Add New Topic</a>';
print '<br><br><br><br><br><br><a href="admin.php">Control Panel</a>' ;
}
else{
header("Location: http://www.forum.com/logout.php");
}
?>
</body>
</html>
<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>
<?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($u=='admin'){
if(($row['name']== $u) and ($row['pass']==$encpass)){
$t=2;
}
}
}
if($t==1){
$_SESSION['user']=$u;
$_SESSION['pass']=$p;
$query = "UPDATE users SET loggedin=1 WHERE name='$u' AND pass='$p' ";
$res= mysql_query ($query)
or die ("Query failed");
echo '<meta http-equiv="Refresh" content="2; url=http://www.forum.com/home.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;
}
if($t==2){
$_SESSION['user']=$u;
$_SESSION['pass']=$p;
echo '<meta http-equiv="Refresh" content="2; url=http://www.forum.com/home1.php" />';
print "<center> Thank you for logging in, $u <br>
Click here if your browser does not automatically redirect you.</center>";
}
?>
</body>
</html>
<?php
session_start();
unset($_SESSION['user']);
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
echo '<center><meta http-equiv="Refresh" content="2; url=http://www.forum.com/index.php" />';
echo 'You Have Been Logged out .. You Have To Signin Again</center>';
?>
</body>
</html>
<?php
session_start();
?>
<html>
<meta http-equiv="Refresh" content="300; url=http://www.forum.com/logout.php" />
<head>
<title></title>
</head>
<body>
<?php
echo $_SESSION['user'];
?>
<form action="addtopic.php" method="post">
Subject : <input type="text" name="sub"><br>
Description :<textarea name="desc" col="400" row="200"></textarea><br>
<input type="submit" value="Confirm new topic">
</body>
</html>
<?php
session_start();
?>
<html>
<meta http-equiv="Refresh" content="300; url=http://www.forum.com/logout.php" />
<head>
<title></title>
</head>
<body>
<?php
if(isset($_SESSION['user'])){
$idt=$_REQUEST['idt'];
$dbh=@mysql_connect('localhost','root','')
or die('unable to Connect');
$dbh=@mysql_select_db('ijdb')
or die('Data base not found');
$sql="SELECT * FROM topics";
$res=@mysql_query($sql)
or die('Query failed');
while($row=mysql_fetch_array($res)){
if($idt == $row['id']){
$topic=$row['subject'];
}
}
echo ' <b><a href=index.php>Abdullah Forum</a> > <a href=home.php>Home</a> > <a href=detailtopic.php>'.$topic.'</a> > <a href=new_reply.php>Reply to thread</a><b><br><br>';
print "Reply To Thread
Thread: $topic<br><br>";
}
else
{
header("Location: http://www.forum.com/logout.php");
}
?>
<form action="add_reply.php" method="post">
Title <input type="text" name="title"><br>
<input type="hidden" name="idt" value="<?php echo $idt; ?>">
Message <br><textarea row="800" col="800" name="msg">
</textarea><br>
<input type="submit" value="Submit Reply">
</form>
</body>
</html>
<?php
session_start();
?>
<html>
<meta http-equiv="Refresh" content="300; url=http://www.forum.com/logout.php" />
<head>
<title></title>
</head>
<body>
<?php
$username=$_REQUEST['username'];
$pass=$_REQUEST['pass'];
$fn=$_REQUEST['fn'];
$ln=$_REQUEST['ln'];
$email=$_REQUEST['em'];
$tel=$_REQUEST['tel'];
$adress=$_REQUEST['adr'];
$city=$_REQUEST['city'];
$country=$_REQUEST['country'];
$encryptpass=md5($pass);
if(isset($_SESSION['user'])){
$link = mysql_connect("localhost", "root", "")
or die ("Could not connect to MySQL");
$db= mysql_select_db ("ijdb")
or die ("Could not select database");
$query = "insert into users (name,pass,firstname,lastname,email,phone,adress,city,country) values ('$username',
'$encryptpass','$fn','$ln','$email','$tel','$adress','$city','$country')";
$result = mysql_query ($query)
or die ("Query failed");
header("Location: http://www.forum.com/index.php");
}
else
{
header("Location: http://www.forum.com/logout.php");
}
?>
</body>
</html>
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.