why this code doesn't work? please help

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
venkata
Forum Newbie
Posts: 1
Joined: Fri Jun 05, 2009 8:38 am

why this code doesn't work? please help

Post by venkata »

Hello!
I have just started learning php. please let me know why the following code doesnt work.

Code: Select all

 
<?php
function db_connect($user='wfuser',$password='wfpass', $db='workflow'){
  mysql_connect('localhost', $user, $password)or die('I cannot connect to db: ' . mysql_error());
mysql_select_db($db);
}
db_connect();
$sql="select * from users where username='".$_POST['uname']."'";
$res=mysql_query($sql);
 
if(! $res)[b]// this condition doesnt work. I just want to avoid duplicate account registration[/b]{
$passwords=$_POST['pword'];
$msql="insert into users (username,email,password) values('".$_POST['uname']."','".$_POST['email']."','".$passwords[0]."')";
mysql_query($msql);
mysql_close();
}
echo "Thanks for registering.welcome".$_POST['uname'];
?>
What will be the output of an sql query when no results match. Thanks in advance
Last edited by Benjamin on Fri Jun 05, 2009 11:19 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: why this code doesn't work? please help

Post by akuji36 »

use this for connection
<?php


// 1. Create a database connection
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
die("Database connection failed: " . mysql_error());
}

// 2. Select a database to use
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
?>
and modify below for your select statement:
<?php
$query = "INSERT INTO books (
title, author, section, checkedout, due_date, checkedout_bypatron_name
)
VALUES (
'{$title}', {$author}, {$section},{$checkedout}, {$due_date},
{$checkedout_bypatron_name}
)";
$result = mysql_query($query, $connection);
if ($result) {
// Success!
echo "Data inserted ";
} else {
// Display error message.
echo "<p>Subject creation failed.</p>";
echo "<p>" . mysql_error() . "</p>";
}
?>

Also double check your field names in mysql against those in your php script
Post Reply