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
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
}
?>

Link to comment
Share on other sites

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
}
?>

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.