LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2005, 01:07 PM   #1
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Rep: Reputation: 15
why cant i connect to my database?


What is wrong ? the second form where i insert data into the database fails.
it cant connect to the database



Here is my index.php file.

------------------------------------------------------------------------------------------------------------------

<?php
function texter()
{
global $conn;
$conn = mysql_connect("localhost", "root", "diewin") or die(mysql_error());
mysql_select_db("texter",$conn) or die(mysql_error());
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Sök efter: <input type="text" size="25" name="searchfor" /><br />
<input type="submit" value="Sök!" />
</form>

<?php

if (isset($_POST['searchfor']) && trim($_POST['searchfor']) != '')
{
texter();
$searchfor = addslashes($_POST['searchfor']);
$sql="SELECT title, text FROM texts,details WHERE texts.textid=details.textid AND title LIKE '%".$searchfor."%';";
$result=mysql_query($sql, $conn);

if(mysql_affected_rows()>0)
{
echo "Följande poster hittades:<br /><br />";

while($row = mysql_fetch_array($result))
{
$details = $row['title'];
$texts = $row['text'];
printf("<B>%s</B><br /><textarea name=text COLS=80 ROWS=20>%s<textarea/>", $details, $texts);
}
mysql_free_result($result);
}
else
echo "Inga poster hittades.<br /><br />";
}
?>
Title: <input type="text" name="title"><br>
Date: <input type="text" name="date"><br>
text: <input type="text" name="text"><br>
<input type="Submit">
</form>

<?
$username="root";
$password="die!win";
$database="texter";

$title=$_POST['title'];
$date=$_POST['date'];
$text=$_POST['text'];

mysql_connect($localhost,$root,$diewin);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO details VALUES ('','$title','$date')";
$query = "INSERT INTO details VALUES ('','$text')";
mysql_query($query);

mysql_close();
?>

</html>
----------------------------------------------------------------------------------------------------------------
My database structure:

create database texter;
create table details (textid int not null primary key auto_increment,title varchar(255),date date);
create table texts (textid int not null,text longtext);
 
Old 05-02-2005, 01:11 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
The varaibles you are using in the second connect statement don't exist - why not just connect with the texter function?
 
Old 05-02-2005, 01:12 PM   #3
andreiij
LQ Newbie
 
Registered: Sep 2004
Posts: 4

Rep: Reputation: 0
Shouldnt the query be terminated with a semicolon?

$query = "INSERT INTO details VALUES ('','$title','$date')";

bara ögnade igenom koden lite snabbt
/andy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
connect to Windows Oracle database turbo_acura Linux - Software 1 11-03-2005 12:35 AM
cannot connect to mysql database from php rocordial Linux - Software 2 08-21-2005 02:05 PM
cannot connect to mysql database externally lsimon4180 Linux - Software 17 03-02-2005 12:28 PM
cannot connect to mysql database mrosati Linux - Software 19 07-15-2004 02:40 PM
Using Tora to connect to remote database devinWhalen Linux - Software 0 02-18-2004 03:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration