Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

code not working

Posted on 2009-07-10
9
Medium Priority
?
196 Views
Last Modified: 2012-05-07
i am checking if the table is existed or not.

// this is in 2.php
1) if table does not exists then create the table and show the form with blank record as there is no record yet inserted in the table.

2) if table exists then don't create it , check if there is any record in the table.
            1) if yes then show the record
            2) if no then simply blank record.


// this is in 3.php
3) now if there is no record in the database then insert the record in the database else update the record in the table.

1.php
<form method="post" action="2.php" name="form">
<table border="1">
	<tr><td>Table Name</td><td><input type="text" name="tname" id="tname" /></td></tr>
	<tr><td colspan="2" align="center"><input type="submit"/></td></tr>
</table>
</form>
 
2.php
<?php
$db_host="localhost";
$db_name="newlogin";
$username="root";
$password="";
$db_con=mysql_connect($db_host,$username,$password) or die("connection not build".mysql_error());
$db=mysql_select_db($db_name) or die("database not build".mysql_error());
 
echo $tname = $_REQUEST['tname'];
 
$res = mysql_query(" show tables like '$tname' ");
if(!$res){
        echo $sql= "create table `$tname`(username varchar(255))";
        $qid=mysql_query($sql) or die("could not create".mysql_error());
}else{
        $sql="select count(*) from $tname";
        $qid = mysql_query($sql);
        if($qid > 0) {
		  while($rec = mysql_fetch_array($qid))
				$username_value = $rec['username'];
        }
		else
        	$username_value = '';
}
?>
 
<form action="3.php?tname='".$tname."'" method="post">
<table border="1">
	<tr><td colspan="2" align="center"><h2>Form</h2></td></tr>
	<tr><td>Username</td><td><input type="text" name="username" id="username" value="<?php echo $username_value; ?>" /></td></tr>
	<tr><td colspan="2" align="center"><input type="submit" /></td></tr>
</table>
</form>
 
3.php
<?php
$db_host="localhost";
$db_name="newlogin";
$username="root";
$password="";
$db_con=mysql_connect($db_host,$username,$password) or die("connection not build".mysql_error());
$db=mysql_select_db($db_name) or die("database not build".mysql_error());
 
$username=$_REQUEST['username'];
$tname=$_REQUEST['tname'];
 
$sql = "select * from '$tname'";
$qid = mysql_query($sql) or die("could not execute".mysql_error());
 
if(!qid){
	echo $sql = "insert into `$tname`(username) values('$username') ";
	$qid = mysql_query($sql) or die("could not insert".mysql_error());
}
else{
	$sql = "update `$tname` set username = '".$username."'";
	$qid = mysql_query($sql) or die("could not update".mysql_error());
}
?>

Open in new window

0
Comment
Question by:designersx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:designersx
ID: 24822174
1.php and 2.php are correct but there are errors in 3.php.

it says could not execute in line 57.

lines upto 43 are correct. after wards there is a problem.
0
 
LVL 11

Expert Comment

by:BrianMM
ID: 24822186
Why not use...

CREATE TABLE IF NOT EXISTS tbl_name


0
 
LVL 11

Accepted Solution

by:
BrianMM earned 500 total points
ID: 24822195
it says could not execute in line 57. :

$sql = "select * from '$tname'";

should be:

$sql = "select * from '".$tname."'";
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:designersx
ID: 24822196
i have solved this question. please don't respond this
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24822304
Line 21: $res will only be false if there is an error in the SQL statement. The fact that there are no tables with that name is not an error. Replace line 21 with these three lines:

if(!$res) die(mysql_error());
$row = mysql_fetch_row($res);
if(!$row) { # no table with name $tname was found

Lines 26-27:

        $qid = mysql_query($sql);
        if($qid > 0) {

$qid is a resource (resultset) handle, it does no contain the number of rows found. Change it into this:

        $res = mysql_query($sql);
        if(!$res) die(mysql_error());
        $qid = mysql_result($res,0);
        if($qid > 0) {

Line 59:

if(!qid){

Same problem here, $qid is only false if the SQL statement had an error. You have also forgot the $ character. Change it into this:

$count = mysql_num_rows($qid);
if(!$count){

I'm not sure what you are trying to do with these scripts, but in general, it is better to insert all users in one table named "users" or similar. The approach you are using here prevents you from doing usefull things with your data, for example listing users.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24822307
>> please don't respond

Sorry, too late!
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24822324
Btw, the accepted solution is wrong. You can not have quotes around the table name in a select statement.
0
 
LVL 11

Expert Comment

by:BrianMM
ID: 24822357
damn... i did mean to type the ` ` type quotes :)
0
 
LVL 11

Expert Comment

by:BrianMM
ID: 24822362
or indeed remove altogether.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question