Jump to content

Recommended Posts

i want to connect database but unable :( using this code

 

 

<?php

session_start();

if(isset($_POST['submit']))

{

$link = mysql_connect("localhost","root","");

$db = mysql_select_db('cms',$con);

$email=$_POST['email'];

$name=$_POST['name'];

 $subject=$_POST['subject'];

  $designation=$_POST['designation'];

  $num=$_POST['#'];

  

//$add_type=$_POST['add_type'];

// echo $q=  "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error());

$sql = mysql_query($q);

 $q=  "select * from contactus " or die(mysql_error());

$sql = mysql_query("select * from contactus");

//$res=mysql_fetch_object($sql);

$rows = mysql_num_rows($sql);

?>

Link to comment
https://forums.phpfreaks.com/topic/295122-help-me-to-resolve-error/
Share on other sites

The database connection link is being saved to $link.

$link = mysql_connect("localhost","root","");
 
You need to use the same variable when calling mysql_select_db(). Try changing this:
$db = mysql_select_db('cms',$con);
 
To this:
$db = mysql_select_db('cms',$link);
 
 
Note that there are other problems with the code, but let's start with establishing a database connection.

What does that mean? if you are getting errors then post them in full here.

<?php
session_start();
if(isset($_POST['submit']))
{
$con = mysql_connect("localhost","root","");
$db = mysql_select_db('cms',$con);
$email=$_POST['email'];
$name=$_POST['name'];
 $subject=$_POST['subject'];
  $designation=$_POST['designation'];
  $num=$_POST['#'];
  
//$add_type=$_POST['add_type'];
// echo $q=  "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error());
$sql = mysql_query($q);
 $q=  "select * from contactus " or die(mysql_error());
$sql = mysql_query("select * from contactus");
//$res=mysql_fetch_object($sql);
$rows = mysql_num_rows($sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sessions</title>        
<link type="text/css" rel="stylesheet" href="styles.css" />
</head>
<body  style="margin-top:0px; margin-left: 0px; background-color:#E6E6FA">
<div id="content" style="height:130px; width:1365px; float:center; background-color:#BA55D3" > <img src="images/image.png">  
  <ul id="menu">
  <div id="header2">
  <li><a href="home.php"><b>Home</b><a></li>
  <li><a href="login.php"><b>Login</b><a></li>
  <li><a href="signup.php"><b>Signup</b><a></li>
<li><a href="about.php"><b>About</b><a></li>
<li><a href="contact us.php"><b>Contact Us</b></a></li>
  </ul> 
  </div>
</div>
<div align="center">
<form method="post" action=""> 
<h2>DETAIL OF OFFICERS OF CS&&IT DEPARTMENT</h2></div>
 
            </br></br>
<table id="abc" border="1"  align="center" bgcolor="white" style="width:60%">
<?php
while($rows=mysql_fetch_array($sql)){
?>
<?php echo $row[0]; ?><br />
<?php echo $row[1]; ?><br />
<?php echo $row[2]; ?><br />
 
<?php } ?> </div>
 
 
 
 
</table>
</form>
            
</html>

 

The database connection link is being saved to $link.

$link = mysql_connect("localhost","root","");
 
You need to use the same variable when calling mysql_select_db(). Try changing this:
$db = mysql_select_db('cms',$con);
 
To this:
$db = mysql_select_db('cms',$link);
 
 
Note that there are other problems with the code, but let's start with establishing a database connection.

 

tried but do not work :(

 

 

<?php
session_start();
if(isset($_POST['submit']))
{
$con = mysql_connect("localhost","root","");
$db = mysql_select_db('cms',$con);
$email=$_POST['email'];
$name=$_POST['name'];
 $subject=$_POST['subject'];
  $designation=$_POST['designation'];
  $num=$_POST['#'];
  
//$add_type=$_POST['add_type'];
// echo $q=  "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error());
$sql = mysql_query($q);
 $q=  "select * from contactus " or die(mysql_error());
$sql = mysql_query("select * from contactus");
//$res=mysql_fetch_object($sql);
$rows = mysql_num_rows($sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sessions</title>        
<link type="text/css" rel="stylesheet" href="styles.css" />
</head>
<body  style="margin-top:0px; margin-left: 0px; background-color:#E6E6FA">
<div id="content" style="height:130px; width:1365px; float:center; background-color:#BA55D3" > <img src="images/image.png">  
  <ul id="menu">
  <div id="header2">
  <li><a href="home.php"><b>Home</b><a></li>
  <li><a href="login.php"><b>Login</b><a></li>
  <li><a href="signup.php"><b>Signup</b><a></li>
<li><a href="about.php"><b>About</b><a></li>
<li><a href="contact us.php"><b>Contact Us</b></a></li>
  </ul> 
  </div>
</div>
<div align="center">
<form method="post" action=""> 
<h2>DETAIL OF OFFICERS OF CS&&IT DEPARTMENT</h2></div>
 
            </br></br>
<table id="abc" border="1"  align="center" bgcolor="white" style="width:60%">
<?php
while($rows=mysql_fetch_array($sql)){
?>
<?php echo $row[0]; ?><br />
<?php echo $row[1]; ?><br />
<?php echo $row[2]; ?><br />
 
<?php } ?> </div>
 
 
 
 
</table>
</form>
            
</html>

Then what should i do now? will u please help me? 

 

Basically, most of your database stuff happens here:

<?php
session_start();
if(isset($_POST['submit']))
{
     $con = mysql_connect("localhost","root","");
     $db = mysql_select_db('cms',$con);
     $email=$_POST['email'];
     $name=$_POST['name'];
     $subject=$_POST['subject'];
     $designation=$_POST['designation'];
     $num=$_POST['#'];
 
     //$add_type=$_POST['add_type'];
     // echo $q=  "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error());
     $sql = mysql_query($q);
     $q=  "select * from contactus " or die(mysql_error());
     $sql = mysql_query("select * from contactus");
     //$res=mysql_fetch_object($sql);
     $rows = mysql_num_rows($sql);
}
?>
 
 
Note that the code only executes after the form submits. And then you have the following code which executes whether or not the form has been submitted:

<?php
while($rows=mysql_fetch_array($sql)){
?>
<?php echo $row[0]; ?><br />
<?php echo $row[1]; ?><br />
<?php echo $row[2]; ?><br />
 
<?php } ?>

 

So...$sql is not going to be defined until the form is submitted.

 

Also note that the following lines are all messed up:

//$add_type=$_POST['add_type'];
// echo $q=  "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error());
$sql = mysql_query($q);
$q=  "select * from contactus " or die(mysql_error());
$sql = mysql_query("select * from contactus");
//$res=mysql_fetch_object($sql);
$rows = mysql_num_rows($sql);
 
First, you're using $q before it's defined. The " or die(..." part needs to be connected with mysql_query. Then you use mysql_query() again which doesn't use $q.

Your code is wrong. You cannot or its not logical to put db connection within an isset function

 

wrong code:

<?php
session_start();
if(isset($_POST['submit']))
{
     $con = mysql_connect("localhost","root","");
     $db = mysql_select_db('cms',$con);
     $email=$_POST['email'];
     $name=$_POST['name'];
     $subject=$_POST['subject'];
     $designation=$_POST['designation'];
     $num=$_POST['#'];
 
     //$add_type=$_POST['add_type'];
     // echo $q=  "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error());
     $sql = mysql_query($q);
     $q=  "select * from contactus " or die(mysql_error());
     $sql = mysql_query("select * from contactus");
     //$res=mysql_fetch_object($sql);
     $rows = mysql_num_rows($sql);
}
?>

Right code:

<?php
session_start();
$con = mysql_connect("localhost","root","");
     $db = mysql_select_db('cms',$con);
if(isset($_POST['submit']))
{
     
     $email=$_POST['email'];
     $name=$_POST['name'];
     $subject=$_POST['subject'];
     $designation=$_POST['designation'];
     $num=$_POST['#'];
 
     //$add_type=$_POST['add_type'];
     // echo $q=  "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error());
     $sql = mysql_query($q);
     $q=  "select * from contactus " or die(mysql_error());
     $sql = mysql_query("select * from contactus");
     //$res=mysql_fetch_object($sql);
     $rows = mysql_num_rows($sql);
}
?>

Preferably

include or require_once (dbcon.php)

//if isset($_POST) goes here:

hope this helps

Your code is wrong. You cannot or its not logical to put db connection within an isset function

 

wrong code:

<?php
session_start();
if(isset($_POST['submit']))
{
     $con = mysql_connect("localhost","root","");
     $db = mysql_select_db('cms',$con);
     $email=$_POST['email'];
     $name=$_POST['name'];
     $subject=$_POST['subject'];
     $designation=$_POST['designation'];
     $num=$_POST['#'];
 
     //$add_type=$_POST['add_type'];
     // echo $q=  "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error());
     $sql = mysql_query($q);
     $q=  "select * from contactus " or die(mysql_error());
     $sql = mysql_query("select * from contactus");
     //$res=mysql_fetch_object($sql);
     $rows = mysql_num_rows($sql);
}
?>

Right code:

<?php
session_start();
$con = mysql_connect("localhost","root","");
     $db = mysql_select_db('cms',$con);
if(isset($_POST['submit']))
{
     
     $email=$_POST['email'];
     $name=$_POST['name'];
     $subject=$_POST['subject'];
     $designation=$_POST['designation'];
     $num=$_POST['#'];
 
     //$add_type=$_POST['add_type'];
     // echo $q=  "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error());
     $sql = mysql_query($q);
     $q=  "select * from contactus " or die(mysql_error());
     $sql = mysql_query("select * from contactus");
     //$res=mysql_fetch_object($sql);
     $rows = mysql_num_rows($sql);
}
?>

Preferably

include or require_once (dbcon.php)

//if isset($_POST) goes here:

hope this helps

 

 

 

thanx but do not work :(

 

Basically, most of your database stuff happens here:

<?php
session_start();
if(isset($_POST['submit']))
{
     $con = mysql_connect("localhost","root","");
     $db = mysql_select_db('cms',$con);
     $email=$_POST['email'];
     $name=$_POST['name'];
     $subject=$_POST['subject'];
     $designation=$_POST['designation'];
     $num=$_POST['#'];
 
     //$add_type=$_POST['add_type'];
     // echo $q=  "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error());
     $sql = mysql_query($q);
     $q=  "select * from contactus " or die(mysql_error());
     $sql = mysql_query("select * from contactus");
     //$res=mysql_fetch_object($sql);
     $rows = mysql_num_rows($sql);
}
?>
 
 
Note that the code only executes after the form submits. And then you have the following code which executes whether or not the form has been submitted:

<?php
while($rows=mysql_fetch_array($sql)){
?>
<?php echo $row[0]; ?><br />
<?php echo $row[1]; ?><br />
<?php echo $row[2]; ?><br />
 
<?php } ?>

 

So...$sql is not going to be defined until the form is submitted.

 

Also note that the following lines are all messed up:

//$add_type=$_POST['add_type'];
// echo $q=  "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error());
$sql = mysql_query($q);
$q=  "select * from contactus " or die(mysql_error());
$sql = mysql_query("select * from contactus");
//$res=mysql_fetch_object($sql);
$rows = mysql_num_rows($sql);
 
First, you're using $q before it's defined. The " or die(..." part needs to be connected with mysql_query. Then you use mysql_query() again which doesn't use $q.

 

 

Thanx For You rEffort But did'nt work :(

Thanx For You rEffort But did'nt work :(

 

It might help to see what you tried. Please surround the code with


tags. It's makes your post and code easier to follow.  :happy-04:

 

Note that my post didn't change any of the code...I was just pointing out some of the inadequacies.

Edited by cyberRobot

 

It might help to see what you tried. Please surround the code with


tags. It's makes your post and code easier to follow.  :happy-04:

 

Note that my post didn't change any of the code...I was just pointing out some of the inadequacies.

 

 

Look how i show table in the center of the page this is my                           display.php

 

 

 

 

<code><?php

error_reporting(0);
$name=$_POST ['name'];
$password=$_POST ['pass'];
$sel=$_REQUEST['sel'];
echo $sel;
   session_start();
   if( !isset($_SESSION['username'])){
  header('Location:login.php');
  die();
   }
 ?>
   <?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("ss1");
$query=mysql_query("select * from user where password='$sel'");
 
 
?>
<html>
<head>
<style>
table
{
margin-left:350px;
margin-top:200px;
text-align:center;
 
}
 
th{
background-color:gainsboro;
}
tr:hover{
background-color: #00FF66;
 
}
h1{
color:red; 
 
tab-size: 10px;
 
font-family: initial;
font-style: oblique;
font-variant: small-caps;
font-weight: bolder;
 
 
}
</style>
<title>Table page</title>
</head>
 
<body bgcolor="pink">
<?php
if(isset($_REQUEST['sel']))
{
?>
<table cellspacing="50" border="1" bgcolor="#CCCCCC" bordercolor="#CC3300">
<tr><th>name</th><th>password</th><th>City</th></tr>
<?php while($row=mysql_fetch_array($query)) {?>
<tr><td><?php echo $row['0'];?></td>
<td><?php echo $row[1];?></td>
<td><?php echo $row[2];?></td>
</tr>
 
<?php } ?>
</table>
<?php } ?>
<form method="post" action="">
<select name="sel">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
 
</select>
<input type="submit"/>
</form>
<a href="logout.php">Logout</a>
</body>
</html>
 
 

</code>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.