Log in

View Full Version : Need help in PHP post forms to MySQL



Total_me
08-22-2009, 02:28 PM
Hello,
I wrote that code on my website:

<form action="comments.php" method="post">
<input type="hidden" name="id" value="NULL" />
<input type="hidden" name="data" value="NULL" /></p>
<p>Komentaras: <br />
<input name="Komentarai" height="50px" type="text" value="" size="60" maxlength="5000" />
<br />
<input type="submit" value="Komentuoti"/>
</form>
in another file comments.php i wrote:

<?
$DBazes_serveris = "xxxxx.xxxxxxx.com";
$DBazes_database = "xxxxxx_xxxx";
$DBazes_vartotojas = "xxxxx_xxxx";
$DBazes_slaptazodis = "xxxxxxx";
$DBazes_lentele = "PHPMySQL";
//////////////////////////////////////////// Prisijungimui
$login = mysql_connect($DBazes_serveris, $DBazes_vartotojas, $DBazes_slaptazodis) or die ('Nepavyko prisijunkti');
//////////////////////////////////////////// LENTELE
@mysql_select_db($DBazes_lentele);
$irasymas_i_db = "INSERT INTO $DBazes_lentele VALUES ( '$id','$Komentarai', '$data') ";
$rezultatas = mysql_query($irasymas_i_db);
mysql_close();

print "
Jusu komentaras: $Komentarai
";
?>
These codes i think have mistakes, so If you could help to fix my problem please tell me ;(
Or if you know another way to post form info to MySQL please tell me, cause I want to learn how to post and get all info to show on php file.

JShor
08-22-2009, 02:45 PM
You're not calling the function mysql_query();, you're only associating a variable with it. Additionally, you need to have the 'id' as your PRIMARY_KEY on your MySQL system, and set it to AUTO_INCREMENT, as INTEGER.

This will fix the query porblem for oyu:


<?
$DBazes_serveris = "xxxxx.xxxxxxx.com";
$DBazes_database = "xxxxxx_xxxx";
$DBazes_vartotojas = "xxxxx_xxxx";
$DBazes_slaptazodis = "xxxxxxx";
$DBazes_lentele = "PHPMySQL";
//////////////////////////////////////////// Prisijungimui
$login = mysql_connect($DBazes_serveris, $DBazes_vartotojas, $DBazes_slaptazodis) or die ('Nepavyko prisijunkti');
//////////////////////////////////////////// LENTELE
@mysql_select_db($DBazes_lentele);
$irasymas_i_db = "INSERT INTO $DBazes_lentele VALUES ( '$id','$Komentarai', '$data') ";
$rezultatas = mysql_query($irasymas_i_db);
mysql_close();

echo "Jusu komentaras:".$Komentarai;

?>


HTH:)

Total_me
08-22-2009, 02:55 PM
Kk i find out. If any1 wants to know how had to be here you go. I hope it will be great example:
[html]<form action="comments.php" method="post">
<input type="hidden" name="id" value="NULL" />
<input type="hidden" name="data" value="NULL" /></p>
<p>Komentaras: <br />
<input name="Komentarai" height="50px" type="text" value="" size="60" maxlength="5000" />
<br />
<input type="submit" value="Komentuoti"/>
</form>[html]
comments.php

<?
$DBazes_serveris = "xxxxxxxxxx"; ///////// Server, host
$DBazes_database = "xxxx_mysql"; ///////// DataBase
$DBazes_vartotojas = "xxxx_mysql"; ////////// Username
$DBazes_slaptazodis = "xxxxx"; ////////// Password
$DBazes_lentele = "table"; /////// Table name
//////////////////////////////////////////// Prisijungimui
$con = mysql_connect("$DBazes_serveris","$DBazes_vartotojas","$DBazes_slaptazodis");
if (!$con)
{
die('Nepavyko prisijunkti: ' . mysql_error());
}

mysql_select_db("$DBazes_database", $con);

$sql="INSERT INTO table(id, Komentarai, data)
VALUES
('$_POST[id]','$_POST[Komentarai]','$_POST[data]')";

if (!mysql_query($sql,$con))
{
die('Klaida: ' . mysql_error());
}
echo "Irasyta"; /// Recorded

mysql_close($con)
?>

Total_me
08-22-2009, 02:58 PM
You're not calling the function mysql_query();, you're only associating a variable with it. Additionally, you need to have the 'id' as your PRIMARY_KEY on your MySQL system, and set it to AUTO_INCREMENT, as INTEGER.

This will fix the query porblem for oyu:


<?
$DBazes_serveris = "xxxxx.xxxxxxx.com";
$DBazes_database = "xxxxxx_xxxx";
$DBazes_vartotojas = "xxxxx_xxxx";
$DBazes_slaptazodis = "xxxxxxx";
$DBazes_lentele = "PHPMySQL";
//////////////////////////////////////////// Prisijungimui
$login = mysql_connect($DBazes_serveris, $DBazes_vartotojas, $DBazes_slaptazodis) or die ('Nepavyko prisijunkti');
//////////////////////////////////////////// LENTELE
@mysql_select_db($DBazes_lentele);
$irasymas_i_db = "INSERT INTO $DBazes_lentele VALUES ( '$id','$Komentarai', '$data') ";
$rezultatas = mysql_query($irasymas_i_db);
mysql_close();

echo "Jusu komentaras:".$Komentarai;

?>


HTH:)

Yes it is ;) Thank you man! You helped me not 1st time ;) But.. Maybe can you write a code how to show info out of DB? To show DB info (posted info) in my php file? Cause i cant imagine how to do it

JShor
08-22-2009, 03:31 PM
You can use mysql_fetch_array() in a while{} statement to show each record of db.

Example:


<?php

$qq = mysql_query("SELECT * FROM $DBazes_lentele") or die(mysql_error());

while($r = mysql_fetch_array( $qq )) {
echo $r[id]."<br>";
echo $r[val]."<br>";
echo $r[val]."<br>";
}

?>


Ref:
http://php.net/while
http://php.net/mysql_fetch_array
http://php.net/mysql_query

Those should help you out.

HTH:)

Total_me
08-22-2009, 03:46 PM
You can use mysql_fetch_array() in a while{} statement to show each record of db.

Example:


<?php

$qq = mysql_query("SELECT * FROM $DBazes_lentele") or die(mysql_error());

while($r = mysql_fetch_array( $qq )) {
echo $r[id]."<br>";
echo $r[val]."<br>";
echo $r[val]."<br>";
}

?>


Ref:
http://php.net/while
http://php.net/mysql_fetch_array
http://php.net/mysql_query

Those should help you out.

HTH:)


Thank you again ;) I added you to friends. You pro?