Jump to content

[SOLVED] Problem submitting data to database


tony_leic

Recommended Posts

First, just to say hello to everyone  ;D

 

I am brand new to php so getting a bit mixed up.

 

i am trying to do something most of you might find real simple

 

i have set my database up correct and am trying to add a bit of info to it, read around the net but can't find my problem, can you help?

 

this is my html form

 

<html><body>

<h4>test</h4>

<form action="process.php" method="post">

Message: <input message="message" type="text" />

<input type="submit" />

</form>

</body></html>

 

this is my process php

 

<?php

// Open database connection

mysql_connect("localhost", "*********", "**********") or die(mysql_error());

mysql_select_db("*******8") or die(mysql_error());

$name = $_POST['message'];

// Insert data

$query = "INSERT INTO friends (name) VALUES ('$name',)";

mysql_query($query);

print "updated";

?>

 

can you see where i have gone wrong as its only showing the data i added when i set the database up?

 

thanks for your help!

 

Tony

Link to comment
Share on other sites

<html><body>
<h4>test</h4>
<form action="process.php" method="post"> 
Message: <input name="message" type="text" /> 
<input type="submit" />
</form>
</body></html>

 

you had message="message" needed to be name="message"

 

also

 

<?php
// Open database connection
mysql_connect("localhost", "*********", "**********") or die(mysql_error()); 
mysql_select_db("*******8") or die(mysql_error()); 
$name = mysql_real_escape_string($_POST['message']);
// Insert data
$query = "INSERT INTO friends (name) VALUES ('$name')";
mysql_query($query) or die(mysql_error());
print "updated"; 
?>

Link to comment
Share on other sites

You have a comma after your $name var in the query, shouldn't be there. You should also you mysql_real_escape_string() on all variables before insertion. Lastly, you should put a die error at the end of the query to catch errors.

 

<?php
// Open database connection
mysql_connect("localhost", "*********", "**********") or die(mysql_error());
mysql_select_db("*******8") or die(mysql_error());

$name = mysql_real_escape_string($_POST['message']);

// Insert data
$query = "INSERT INTO friends (name) VALUES ('$name')";
$result = mysql_query($query)or die(mysql_error());

print "updated";
?>

Link to comment
Share on other sites

wow fast!

 

so now i have got

 

html =

 

<html><body>

<h4>test</h4>

<form action="process.php" method="post">

Message: <input message="message" type="text" />

<input type="submit" />

</form>

</body></html>

 

php =

 

<?php

mysql_connect("localhost", "****", "****") or die(mysql_error());

 

 

mysql_select_db("****") or die(mysql_error());

 

$name = mysql_real_escape_string($_POST['message']);

 

// Insert data

$query = "INSERT INTO friends (name) VALUES ('$name')";

$result = mysql_query($query)or die(mysql_error());

 

print "updated";

?>

 

but still don't work!

 

think i messed something up?

 

Link to comment
Share on other sites

right.. hehe

 

this is what i set my database up with

 

CREATE TABLE friends (name VARCHAR(30), message VARCHAR(30));

 

INSERT INTO friends VALUES ( "123", "456")

 

this is what i am reading it with

 

<?php

header( 'refresh: 5; url=******' );

// Connects to your Database

mysql_connect("localhost", "*****", "*****") or die(mysql_error());

mysql_select_db("*****") or die(mysql_error());

// Collects data from "friends" table

$data = mysql_query("SELECT * FROM friends") or die(mysql_error());

// puts the "friends" info into the $info array

$info = mysql_fetch_array( $data );

// Print out the contents of the entry

Print "<b>Message:</b> ".$info['name'] . " <br>";

?>

 

this is my html form as i have it now

 

<html><body>

<h4>test</h4>

<form action="process.php" method="post">

Message: <input name="message" type="text" />

<input type="submit" />

</form>

</body></html>

 

and this is what i am trying to write to it with now

 

<?php

mysql_connect("localhost", "*****, "******") or die(mysql_error());

mysql_select_db("******") or die(mysql_error());

$name = mysql_real_escape_string($_POST['message']);

// Insert data

$query = "INSERT INTO friends (name) VALUES ('$name')";

$result = mysql_query($query)or die(mysql_error());

print "updated";

?>

 

and still not working lol

 

maybe i should give up lol

 

Tony

Link to comment
Share on other sites

Are you sure the php page is called "process.php"? What exactly is happening, are you getting a blank page, error, or what?

 

Yep its called process.php but no, no error or blank

 

i am uploading it to the server

going to the page, writing something in the field, hit submit, it then goes to the php and says "updated" ok like at the bottom of the php file

 

the thing i made to read it is auto refreshing every 5 seconds like it should but not showing the new data i just sent to the database.

 

all i am doing is trying to learn and i thought just to be able to write then read from the database and have it show on the page would be good ........ but nooooooooooo my brain refuses to get around this  :-\

Link to comment
Share on other sites

Oh, okay...I see.

 

Change the file you are reading it with to this

 

<?php
header( 'refresh: 5; url=******' );

// Connects to your Database
mysql_connect("localhost", "*****", "*****") or die(mysql_error());
mysql_select_db("*****") or die(mysql_error());

// Collects data from "friends" table
$data = mysql_query("SELECT * FROM friends") or die(mysql_error());

// puts the "friends" info into the $info array
while ($info = mysql_fetch_array($data)){
   // Print out the contents of the entry
   Print "Message: ".$info['name']."<p>";
}

?>

Link to comment
Share on other sites

Then you will want an UPDATE query instead of an INSERT one. You are going to have to add an auto-increment field though, or else you won't know the condition to update the table by.

 

So add a field into that table called "friendID" or soemething, and make it an auto-increment and primary key.

 

Now change your code to this:

 

<?php
header( 'refresh: 5; url=******' );

// Connects to your Database
mysql_connect("localhost", "*****", "*****") or die(mysql_error());
mysql_select_db("*****") or die(mysql_error());

// Collects data from "friends" table
$data = mysql_query("SELECT * FROM friends ORDER BY friendID DESC") or die(mysql_error());

// puts the "friends" info into the $info array
$info = mysql_fetch_assoc($data);

Print "Message: ".$info['name'];

?>

 

 

Link to comment
Share on other sites

Thanks for that positive post think i need it, i thought maybe my learning capabilities where just slowing down :D

 

I used to write quite advanced programs for the older pc's when younger like commodor, spectrum and amstrad, i can do so much html and i became an advanced scripter in SL but php omg! has to be the hardest one yet

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.