LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 02:07 PM   #1
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Rep: Reputation: 15
Nothing happens with my database :((


This is my cgi script
i have linked it to my php.index file
but when i try to insert things to my database threw the browser norhing happens...
whats wrong-?




#!/bin/bash


TITLE=$(echo $QUERY_STRING | cut -d\& -f1 | cut -d= -f2)
DATE=$(echo $QUERY_STRING | cut -d\& -f2 | cut -d= -f2)
TEXT=$(echo $QUERY_STRING | cut -d\& -f3 | cut -d= -f2)
KOLL=$(echo "SELECT count(title) FROM details,texts WHERE details.textid=texts.textid AND title=\"$TITLE\";"| mysql -N -u root -p "diewin" texter)

#Writing a HTTP-Header
echo "content-type: text/html"
echo

#Writing a HTML-Header
echo "<HTML><HEAD><TITLE>Resultat</TITLE></HEAD><BODY>"

if [ $KOLL -ne 0 ]; then
echo "<h4 style=\"color: #fff\">Titeln $TITLE finns redan!.</h4>"

else

echo "INSERT INTO details (title,date) VALUES (\"$TITLE\",\"$DATE\");" | mysql -N -u root -p "diewin" texter
echo "INSERT INTO texts(text) VALUES (\"$TEXT\");" | mysql -N -u root -p "diewin" texter
echo "Titeln $TITLE och dess text är tillagd"
fi

echo "SELECT title FROM texter;" | mysql -N -u root -p diewin texter > /tmp/sqlsvar
IFS=$'\n'
echo "<P>Dessa Titlar finns i tabellen</P>"
echo "<ul>"
for i in $(cat /tmp/sqlsvar); do
echo "<li>$i</li>"
done
echo "</ul>"

# skriv HTML-slute
echo "</body></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, 02:53 PM   #2
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Rep: Reputation: 30
My first question is why is this in bash? Why not just write it in PHP?

Secondly, edit your original post and remove your root password!
 
Old 05-02-2005, 03:34 PM   #3
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Original Poster
Rep: Reputation: 15
dude why cant you just try to help me? i didnt get it to work in php thats why.
and ofcourse the root password is not the password im using its just an example...
 
Old 05-02-2005, 03:44 PM   #4
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Rep: Reputation: 30
What were your problems with PHP? That might give a clue as to your current problem.

How exactly did you 'link' this script from your php page? passthru(), exec() system() ?

(I assume php.index is a typo?)
 
Old 05-02-2005, 03:51 PM   #5
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Not to mention you have a space between the "-p" and the password. The proper syntax is -pPASSWORD.
 
Old 05-02-2005, 07:30 PM   #6
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Original Poster
Rep: Reputation: 15
Iv allready posted this one tho, but here is my php version.
wich is didnt get to work cuse it wont connect to the database this time either...



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>

<?

$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, 08:42 PM   #7
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Rep: Reputation: 30
If it's not connecting to the database that's a bigger problem than the programming language. What were your errors? Can you connect in a shell?
 
Old 05-02-2005, 08:47 PM   #8
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Original Poster
Rep: Reputation: 15
the search form works perfectly except it shows some html code in its answers but that doesnt matter...

its just the insert form that doesnt connect to the database.

to circumvent any missunderstandings:
Yes MySQL is online and working
Yes Apache2 is online and working
Yes Apache2 has support for both MySQL and PHP

So if anyone could take a look on the insert form at the bottom of the index.php file i showed you cuse thats where the problem is

thanks
 
Old 05-02-2005, 09:03 PM   #9
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Rep: Reputation: 30
Firstly, you need the superglobal to access POSTed form data, like so:
PHP Code:
$query "INSERT INTO details VALUES ('',$_POST['title'],$_POST['date'])";
$query "INSERT INTO details VALUES ('',$_POST['text'])";
mysql_query($query); 
Secondly, the second line sets $query to insert into details '', text. The first query is never executed.

Thirdly, you never explictly state which rows to insert data into. If you are specifying only a few of the rows in a table you must do so in the query:
PHP Code:
$query "INSERT INTO details (id, title, date) VALUES ('',$_POST['title'],$_POST['date'])"
$query "INSERT INTO details (text) VALUES ('',$_POST['text'])" 
Why would you split that up into two queries though?

Last edited by michaelsanford; 05-02-2005 at 09:06 PM.
 
Old 05-02-2005, 09:45 PM   #10
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Umm, your code has
Code:
mysql_connect($localhost,$root,$diewin);
Don't you mean
Code:
mysql_connect("localhost","root","diewin");
?
 
Old 05-02-2005, 10:22 PM   #11
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Original Poster
Rep: Reputation: 15
Okay i have split the code into two files insert.php and index.php

Whats wrong with my insert.php file i cant get it to work
the error message:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/localhost/htdocs/insert.php on line 21

Whats wrong with the code guys


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




<?php



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

$query = "INSERT INTO details (textid, title, date) VALUES ('',$_POST['title'],$_POST['date'])"
$query = "INSERT INTO details (text) VALUES ('',$_POST['text'])"
mysql_query($query);
mysql_close();
?>
----------------------------------------------------------------------------------------------------------------------------------
 
Old 05-02-2005, 11:49 PM   #12
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Rep: Reputation: 30
Missing ; after query strings. Also like I asked before, why are you splitting up those two query strings? It's only going to execute the second query.

Last edited by michaelsanford; 05-02-2005 at 11:52 PM.
 
Old 05-03-2005, 07:38 AM   #13
Alexander.s
Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Slackware, Gentoo!
Posts: 115

Original Poster
Rep: Reputation: 15
How do i insert everything on one row?

im just guessing here:

$query = "INSERT INTO details,texts (textid, title, date),(textid, text) VALUES ('',$_POST['title'],$_POST['date'])",('',$_POST['text'])";
 
Old 05-03-2005, 09:28 AM   #14
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
You need to use '.', the concatenation operator, not ',', the list operator.

And that query you posted makes no sense: you can only insert to one database.

For example:
Code:
$query = "INSERT INTO details (textid, title, date) VALUES(''.$_POST['title'].','.$_POST['date'].','.$_POST['text']")";
Though I do notice these strings are out of order, or seem to be.
 
Old 05-03-2005, 03:25 PM   #15
michaelsanford
Member
 
Registered: Feb 2005
Location: Ottawa/Montréal
Distribution: Slackware + Darwin (MacOS X)
Posts: 468

Rep: Reputation: 30
PHP Code:
$query_details "INSERT INTO details (textid, title, date) VALUES('', $_POST['title'], $_POST['date'])";
$query_text "INSERT INTO text (textid, text) VALUES((SELECT LAST_INSERT_ID()), $_POST['text']")";
mysql_query(
$query_details);
mysql_query(
$query_text); 
You will NEED to use the subquery SELECT LAST_INSERT_ID() on the second query so that you insert the same textid into the second query. Search google for the right info on that, I may have it wrong.

If you get an error on that it's because your MySQL server version doens't support subqueries which were introduced in 4.1 I think.

Matir, you can insert into more than one table at once though which is what he's doing Also we do want the list operator because we're inserting multiple items, not one single row of concatenated values.

Also, if you're having problems with the queries, go into a mysql shell and test them manually, that way you'll get specific error messages. PHP is a last step in database interface design...

Last edited by michaelsanford; 05-03-2005 at 03:28 PM.
 
  


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
Snort database: Closing connection to database "" Homer Glemkin Linux - Security 2 07-14-2005 06:58 PM
Database apffal Linux - General 1 08-09-2004 06:35 PM
database dannyl General 4 08-16-2003 10:50 PM
Database marlaina1 Linux - General 4 06-01-2002 10:58 PM
Database darkness Linux - Distributions 1 04-16-2002 02:17 AM

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

All times are GMT -5. The time now is 01:59 PM.

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