Jump to content

Recommended Posts

Need help ASAP. i need the following form to right to mysql table "table1" and columns "name" "email" and "comment".

<form action="sign.php" method="post" class="pure-form pure-form-stacked">
<fieldset>

<label for="name">Your Name</label>
<input id="name" type="text" placeholder="Your Name" name="name">


<label for="email">Your Email</label>
<input id="email" type="email" placeholder="Your Email" name="email">

<label for="comment">Your Comments</label>
<input id="comment" type="text" placeholder="Your Comments" name="comment">

<a href="thanks.html" button type="submit" class="pure-button">SUBMIT</button>
</a>

</fieldset>
</form>

My PHP File looks like this:

<html>
<body>

<?php
$myUser
= "dbuser";
$myPassd = "********";
$myDB = "dbname";
$myserver = "192.168.1.80:3306";
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];

// Create connection
$dbhandle = mysql_connect($myserver,$myUser,$myPassd)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//if (mysqli_connect_errno()) {
//echo "Failed to connect to MySQL: " . mysqli_connect_error();
//}
//echo "connected";

$sql = "INSERT INTO signatures (name, comment) VALUES ( $name, $comment)";

if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "record added";

mysql_close($dbhandle);
?>
</body>
</html>

I also need this to redirect to another html page after it run.

Like i said any help would be great because i cant get it to work!

Link to comment
https://forums.phpfreaks.com/topic/293220-form-wont-write-to-mysql/
Share on other sites

First and foremost, the mysql driver has been deprecated and will be removed from an upcoming version of PHP, so your code won't work as it's not futureproof when that happens. You really should use PDO or MySQLi driver or you're going to have to rewrite all of your DB stuff in the near future. Might as well do that now before that happens.

 

Do you get any errors from die('Error: ' . mysql_error());??

 

In your query, $name and $comment, assuming they're text, need quotes around them. They should also be escaped before entering them in the db.

i'm going to guess that the symptom you got from your code was that it went to the thanks.html page when you clicked on the submit (link) and that it never actually went to the sign.php page?

 

if so, the reason for that is because your form's submit button is incorrectly coded html. you have a mix of an <a> anchor tag (link) and a button, none of which is valid, and cannot submit a form without the aid of some javascript.

 

it's not really possible to program without first learning the basics of what you are trying to do. you would need to research what the syntax of a form's submit button is before you can use it correctly in your form.

If you want to pretend to be a programmer, you'll have to pretend to learn as well.  Try it.  You might like it.

 

And no - we're not going to throw something together for you.  Even if you need it ASAP.....

 

You might also work on your English too.

Some reasonably simple documentation regards HTML forms. This stuff really is the basic building blocks of web development. If you don't know how to do it and can't be bothered learning then you should be paying a developer, not coming on here looking for freebies. Sorry to be harsh, but most of us have spent many sleepless nights sweating caffeine to learn these things. Most of us want to make the learning path easier for the next person, but we don't want to do the actual work for you. I like to get paid for my professional services.;-)

 

However, your main issue re: form submission, as already pointed out, is that you are trying to submit a form using an <a> tag (which can be done - but is unnecessary in this case). Use an <input type="submit"...> or a <button type="submit"... element to submit the form. Both of them read the destination of the <form> from the action attribute on the <form action="some_page.php"> tag.

 

Here are a couple of links to reputable sites: 

 

info about html forms - http://www.w3schools.com/html/html_forms.asp

 

info about the button element (can be used to submit a form) - http://www.w3schools.com/tags/tag_button.asp

 

And for redirecting pages - http://www.w3schools.com/php/func_http_header.asp

 

Good luck.

Edited by wezhind

w3schools is not a reputable source. It's infamous for security vulnerabilities, bad practices, outdated information and plain nonsense. If you have any doubts about that, just look at their file upload script, for example.

 

Good information can be found at the Mozilla Developer Network. Or buy a good book.

Perhaps I should have said 'more reputable than some' (I was more thinking malware-free when I wrote that) :-) I did a quick check over the pages I referred to myself and they seemed to include all of the information that the OP would require to resolve their issue, with no particular bad practices or outdated info involved. I went for something simple. I didn't think the relevant pages I found on the MDN were particularly newbie-friendly - but that's my opinion.

 

A good book is hard to beat and my favourite medium - it naturally deters the hurried copy-and-paste clone-type 'development' that seems to be employed in a lot of modern coding...but again they go out of date quite quickly... oh it's so tricky!

 

OP: if Jaques1 has reservations about w3schools, I would seriously advise not making it a habit to use info from there, as he indubitably has more knowledge about such things than I (I rarely reference online code-sites anymore) - however, the links I provided should give you what you need to learn to resolve this particular set of issues. As suggested, you may wish to try the Mozilla Developer Network or check Amazon (or your favourite book supplier) for an up-to-date HTML reference manual for future learning.

 

Good luck.

Edited by wezhind
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.