Jump to content

[SOLVED] Simple form with mysql


nadz

Recommended Posts

ok, i am just trying to make a simple form at the moment that logs the users id and name. basically i have a form that asks for the users name and email address, it checks if the email address is registered by checking if there is an "id" present with the email address in my "user" table. I would like it to create an entry in my "crush" table with the users name and id. and if the email address does not match an id it should echo "id incorrect" or the login form. i have this code but it always echoes invalid id, even when i put the right details in:

 

<?php

if (!$_POST)
{
  include("form.php");
}

$dbhost = 'mysql.d****.co.uk';
$dbuser = 'nextman';
$dbpass = '*****';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

$dbname = 'main';
mysql_select_db($dbname);

$fetch=mysql_fetch_assoc(mysql_query("SELECT `id` FROM `user` WHERE `email`='$email'"));
  $id = $fetch['id'];

$id = isset($_GET['id'])?$_GET['id']:0;

	if ($id > 0) {

	$name = $_POST['txtName'];
	$email = $_POST['txtEmail'];

	mysql_query("INSERT INTO `crush` (name, id) VALUES ('$name', '$id')");

	include("member.php");
	}
	else 
	{
		echo 'invalid entry id';
	}

?>

Link to comment
https://forums.phpfreaks.com/topic/58872-solved-simple-form-with-mysql/
Share on other sites

your idea is good except try using (take all you need in the query because if its right it prevents a need for a second query to retrive info (don't use get/post info for you will develop caps issue  also you never define $email maybe you are missing:

<?php
$email = $_POST['email'];
$fetch = mysql_query("SELECT `id`, `username`, `email` FROM `user` WHERE `email`='$email'");
if(mysql_count_rows($fetch) > 0)
{
  //User is already a member load the mysql data
}
else
{
//user is not part of the table so add to table via an insert
}
?>

hi, ive tried using this code instead but im getting a "Call to undefined function mysql_count_rows()" error. i got everything to work using the old code but i thought id try this if its more efficient.

 

your idea is good except try using (take all you need in the query because if its right it prevents a need for a second query to retrive info (don't use get/post info for you will develop caps issue  also you never define $email maybe you are missing:

<?php
$email = $_POST['email'];
$fetch = mysql_query("SELECT `id`, `username`, `email` FROM `user` WHERE `email`='$email'");
if(mysql_count_rows($fetch) > 0)
{
  //User is already a member load the mysql data
}
else
{
//user is not part of the table so add to table via an insert
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.