cmm8907 Posted December 21, 2014 Share Posted December 21, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/293220-form-wont-write-to-mysql/ Share on other sites More sharing options...
CroNiX Posted December 22, 2014 Share Posted December 22, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293220-form-wont-write-to-mysql/#findComment-1500313 Share on other sites More sharing options...
cmm8907 Posted December 22, 2014 Author Share Posted December 22, 2014 im going to be honest becuase this is all new to me so i dont really know how to do any of that. any way you could throw the code together for me and i can past it in and test it? Quote Link to comment https://forums.phpfreaks.com/topic/293220-form-wont-write-to-mysql/#findComment-1500314 Share on other sites More sharing options...
mac_gyver Posted December 22, 2014 Share Posted December 22, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293220-form-wont-write-to-mysql/#findComment-1500396 Share on other sites More sharing options...
ginerjm Posted December 22, 2014 Share Posted December 22, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293220-form-wont-write-to-mysql/#findComment-1500414 Share on other sites More sharing options...
wezhind Posted December 28, 2014 Share Posted December 28, 2014 (edited) 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 December 28, 2014 by wezhind Quote Link to comment https://forums.phpfreaks.com/topic/293220-form-wont-write-to-mysql/#findComment-1500884 Share on other sites More sharing options...
Jacques1 Posted December 28, 2014 Share Posted December 28, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293220-form-wont-write-to-mysql/#findComment-1500912 Share on other sites More sharing options...
wezhind Posted December 28, 2014 Share Posted December 28, 2014 (edited) 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 December 28, 2014 by wezhind Quote Link to comment https://forums.phpfreaks.com/topic/293220-form-wont-write-to-mysql/#findComment-1500913 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.