ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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-?
#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);
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...
<?php
function texter()
{
global $conn;
$conn = mysql_connect("localhost", "root", "diewin") or die(mysql_error());
mysql_select_db("texter",$conn) or die(mysql_error());
}
?>
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);
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.
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());
}
?>
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.
$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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.