Hi,,
My problem is that i have two table city and location.In city table i have 3 field city_id(Primary key),city_name,city_status where city_status is true or false.In location table i have 3 field also loc_id(Primary key),city_id,loc_city.i done a code in which city_name comes into drop down list from city table.my problem starts here when i select one of this value and want to insert into location table...then this show error..is there any code available related to my problem plz send my id......

Recommended Answers

All 19 Replies

post your code .i need to look through

:)

Here is my code which i am using here...........
droplistcity.php

<html>
<head>
<script language="JavaScript">
function onChange() {
  var Current=document.form1.select1.selectedIndex;
  document.form1.currentText3.value=document.form1.select1.options[Current].text;
}
</script>
</head>
<body>
<form name="form1" method="POST">
<?php include("connect.php")?>
<table border="1" align="center">
<tr><td>List of City</td>
<td><select name="select1" onChange="onChange()">
    <option value="">Select City</option>
<?php
 $reza=mysql_query("select city_id,city_name from city order by city_id");
                    while ($row=mysql_fetch_array($reza)){
					   $id1=$row["city_id"];
                      $id2=$row["city_name"];
                      
				 echo "<option value=\"$id2\">$id2</option>";
                    }
mysql_close($con);
?>
</select></td>
</tr>
<tr><td>Selected City</td>
<td><input
  name="currentText3"
  type="text"
  value=""
></td>
</tr></form>
</body>
</html>

cityname.php

<?php include("connect.php");?>
<?php 
$city_name = $_POST['currentText3'];
$result=mysql_query("select * from city where city_name='$city_name'");
while($row = mysql_fetch_array($result))
  {
  echo $row['city_name'] ;
  echo "<br />";
  }
mysql_close($con);
?>

addloc.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form name="form1"action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="1" align="center">
<input type="hidden"  name="l_id" />
<tr>
<?php include("droplistcity.php");?>
</tr>
<tr>
<td>Location Name</td>
<td><input type="text" name="location" /></td></tr>
<tr>
<td></td><td><input type="submit" value="added!">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php include("connect.php");?>
<?php include("cityname.php");?>

<?php
$l_id = $_POST['l_id'];
$city_id = $_POST['city_name'];
$location = $_POST['location'];
$query = "INSERT INTO location (loc_id, city_id,loc_city) VALUES('$l_id',(SELECT city_id FROM city WHERE city_id = '$city_id'),'$location')";
$result = mysql_query($query) or die("Query failed: ".mysql_error());
mysql_close($con);
?>



here is my all code........plz reply as soon as possible.......

Here is my code which i am using here...........
droplistcity.php

<html>
<head>
<script language="JavaScript">
function onChange() {
  var Current=document.form1.select1.selectedIndex;
  document.form1.currentText3.value=document.form1.select1.options[Current].text;
}
</script>
</head>
<body>
<form name="form1" method="POST">
<?php include("connect.php")?>
<table border="1" align="center">
<tr><td>List of City</td>
<td><select name="select1" onChange="onChange()">
    <option value="">Select City</option>
<?php
 $reza=mysql_query("select city_id,city_name from city order by city_id");
                    while ($row=mysql_fetch_array($reza)){
					   $id1=$row["city_id"];
                      $id2=$row["city_name"];
                      
				 echo "<option value=\"$id2\">$id2</option>";
                    }
mysql_close($con);
?>
</select></td>
</tr>
<tr><td>Selected City</td>
<td><input
  name="currentText3"
  type="text"
  value=""
></td>
</tr></form>
</body>
</html>

cityname.php

<?php include("connect.php");?>
<?php 
$city_name = $_POST['currentText3'];
$result=mysql_query("select * from city where city_name='$city_name'");
while($row = mysql_fetch_array($result))
  {
  echo $row['city_name'] ;
  echo "<br />";
  }
mysql_close($con);
?>

addloc.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form name="form1"action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="1" align="center">
<input type="hidden"  name="l_id" />
<tr>
<?php include("droplistcity.php");?>
</tr>
<tr>
<td>Location Name</td>
<td><input type="text" name="location" /></td></tr>
<tr>
<td></td><td><input type="submit" value="added!">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php include("connect.php");?>
<?php include("cityname.php");?>

<?php
$l_id = $_POST['l_id'];
$city_id = $_POST['city_name'];
$location = $_POST['location'];
$query = "INSERT INTO location (loc_id, city_id,loc_city) VALUES('$l_id',(SELECT city_id FROM city WHERE city_id = '$city_id'),'$location')";
$result = mysql_query($query) or die("Query failed: ".mysql_error());
mysql_close($con);
?>



here is my all code........plz reply as soon as possible.......

$query = "INSERT INTO location (loc_id, city_id,loc_city) VALUES('$l_id',(SELECT city_id FROM city WHERE city_id = '$city_id'),'$location')";

Why dont you try simple select query to get the city_id and then insert into location table,

That should work ok? ;)

I also want to see your error mysql prints;

Query failed: Access denied for user 'ODBC'@'localhost' (using password: NO)


when i am accessing file addloc1.php

plz find as soon as possible....

you must set password for your mysql connection.

example:

$host="localhost";
$user="root";
$pass="SeKr1t";

mysql_connect($host,$user,$pass); // You need a password set ok?

thats all ;)

Thanks for your reply but i have tried this one also there was no result.is there any problem on my code or query......

plz reply me....

please get this for me.

1. what is your database engine or type. eg ms sql, mysql, oracle, etc.

2. did you received any errors of any kind.??
a.if yes, post the error
b. if no, make sure you have data in the database for your query.

please advice. ;)

i am using mysql and iis for running application.....all are current version...

(if no, make sure you have data in the database for your query.) what is the meaning of this...........

populate at least some few data in your tables that you query.

if they are empty, you will get no data out.

Do what i told you and post all errors if any.

in my city table there are two entry
city_id 1,2
city_name noida ,lucknow
city_status true,true

in my location table this is empty.....because i am inserting this...

You error must be very simple only i cant understand what you really want.

// select from city

$query= "select city_id from city where( you know what you want)";
//exe query

$result=mysql_query($query) or die();
$row=mysql_fetch_array($rsult));
$city_id=$row['city_id'];
    // then insert into location
if($city_id){
$q= "insert into location values('','$location_nane','$city_id')";
$re=mysql_query($q) or die (mysql_error());
if($re){
echo "roger that";
}
}

this should be out last contact skyhawk 67.
roger that ;)

My problem is not resolved yet when i am running this code a problem is displaying

HTTP Error 500.0 - Internal Server ErrorThe page cannot be displayed because an internal server error has occurred.

what is this error????/

your server is not setup well. This got nothing to do with your mysql stuff.

Did you play around with the apache2 conf or the php ini? ????

need more info :)

Error SummaryHTTP Error 500.0 - Internal Server ErrorThe page cannot be displayed because an internal server error has occurred.Detailed Error InformationModule FastCgiModule
Notification ExecuteRequestHandler
Handler PHP_via_FastCGI
Error Code 0x00000000
Requested URL http://localhost:80/muneer/newzstreet/addloc1.php
Physical Path C:\inetpub\wwwroot\muneer\newzstreet\addloc1.php
Logon Method Anonymous
Logon User Anonymous

HOw can i remove this error?......I am using IIS server not apache.....

o hell no,

I dont remember the last time i used microsoft stuff.

why dont you simply use wamp or xamp.??
well thats your taste. if you care to, just google wamp or xamp and start developing. They are better for php my friend ;)

<?php include("connect.php");?>
<?php include("cityname.php");?>
<?php // select from city
$query= "select city_id from city where(city_name='$city_name')";
//exe query
$result=mysql_query($query) or die();
$row=mysql_fetch_array($rsult);
$city_id=$row['city_id'];
// then insert into location
if($city_id){
$l_id=$_POST['l_id'];
$location=$_POST['location'];
$q = "INSERT INTO location (loc_id,city_id,loc_city)VALUES('','$city_id','$location')";
$re=mysql_query($q) or die (mysql_error());
if($re){
echo "roger that";
}
}
?>

when i am using this code following i find this code

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\newzstreet\addloc1.php on line 29

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\newzstreet\addloc1.php on line 29


currently i am using Apache server

Check if your DB was connected.

The Link to the DB server was not established.

1. You must connect to your mysql server with your hostname, uername, password.

Do that first . ::)

Thanks for your reply, but I also tried result.is had a problem in my code or the implementation of ...... plz answer 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.