I need some help with this php code, please.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pickamaterina
Forum Newbie
Posts: 2
Joined: Sun Sep 29, 2013 1:39 pm

I need some help with this php code, please.

Post by pickamaterina »

<?php
$user="root";
$password="";
$database="base";
$host="localhost";
$table="com";

mysql_connect($host, $user, $password) or die("error");
mysql_select_db($database) or die("error");

$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$coment= $_POST['coment'];
$error = '';

if(empty($name) || empty($lastname) || empty($email) || empty($coment))
{
$error .= 'error. ';
}
if(!preg_match("/^[a-zA-A]+$/i", $name))
{
$error .= 'Error ';
}

if(!preg_match("/^[a-zA-A]+$/i", $lastname))
{
$error .= 'Error ';
}



if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email))
{
$error .= 'errorl. ';
}

if(!preg_match("/^[a-zA-z0-9.,?!]+$/i", $comment))
{
$error .= 'Error ';
}

if($error == '')
{

$result = "INSERT INTO com (name, lastname, email, coment) VALUES('$name', '$lastname', '$email','$coment')";
echo "<script type='text/javascript'>alert('Thanks.'); window.close();</script>";}
else{
echo"<script type='text/javascript'>alert ('$error'); window.close(); </script>";}

Errors seems to be working its just it won't insert data in table in mysql database....

Thank you so much if you could help me.And yes I am beginner, a desperate beginner ...
pickamaterina
Forum Newbie
Posts: 2
Joined: Sun Sep 29, 2013 1:39 pm

Re: I need some help with this php code, please.

Post by pickamaterina »

l I tried connecting $result via if (mysql_query($result)){
echo "<script type='text.....
and it didn't work... sorry I am just a beginner so I can't think of any way how to connect it and leave that if or use it because it is kinda needed for validation?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: I need some help with this php code, please.

Post by Celauran »

Don't use mysql_ functions. Don't blindly insert user data; validate and sanitize. Prepared statements are your friend. Whatever tutorial you're following is outdated and teaching bad practices. Abandon it. Seriously. If you're just learning, start here.
Post Reply