Jump to content

Search for add if not


rusking

Recommended Posts

Im trying to make a php page that searchs for whats typed in a field and if it doesnt exits then add.  I am tring to use whats below but cant get it to work. Any thoughts

 

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="" action="">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Search</strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Search"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>



$myusername = username




<?php
$con = mysql_connect("mysqlserver","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbtest", $con);

mysql_query("INSERT INTO partylist (Username, Key)
VALUES ($myusername , 'keytest')");



mysql_close($con);
?> 

Link to comment
Share on other sites

Ok it runs, returns not erros but never enters the data

<?php
$con = mysql_connect("mysqlserveraddress","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbtest", $con);

mysql_query("INSERT INTO partylist (Username, Key)
VALUES (frank , 'keytest')");



mysql_close($con);
?> 

Link to comment
Share on other sites

using

<?php
$con = mysql_connect("mesqlserver","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbtest", $con);

mysql_query("INSERT INTO partylist (Username, Key)
VALUES ('frank' , 'keytest')");



mysql_close($con);
?>

 

 

 

Still nothing. I provide a screenshoot below of phpadmin to double check me

 

 

 

 

 

 

weird.png

Link to comment
Share on other sites

welp i got the submit part...i used http://www.w3schools.com/PHP/php_mysql_insert.asp and made and insert.php with a forum.html. Still lost as to how to have it search to see if the record exist befor submiting. I think maybe a qury? not sure if thats right or not

 

the html

<html>
<body>

<form action="insert.php" method="post">
Recordname: <input type="text" name="firstname" />
Username: <input type="text" name="lastname" />
ID: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html> 

 

 

And the insert.php

 

<?php
$con = mysql_connect("mysqlserver","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbtest", $con);

$sql="INSERT INTO partylist (RecordID, Username, ID)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?> 

Link to comment
Share on other sites

i know this is prob way off but this is kinda in my mind how i think it would work

 

<?php
$con = mysql_connect("mesqlserver","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbtest", $con);

$query  = "SELECT username, id, message FROM partylist";
if (0) //lol idk, i used 0 becuase i know it means false
{$sql="INSERT INTO partylist (RecordID, Username, ID)
VALUES
('$_POST[firstname]','$_POST[username]','$_POST[id]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Welcome New User";

Else
echo "User already exist"}

mysql_close($con)
?>  

Link to comment
Share on other sites

I got this far but cant figure out why dreamweaver (i switched to that) says the else statment has errors

 

 

<?php
$con = mysql_connect("sqladdress","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbtest", $con);

$query  = "SELECT username, id, message FROM partylist";
if ($query =='username')
  echo "User already exist"

else
$sql="INSERT INTO partylist (RecordID, Username, ID)
VALUES
('$_POST[firstname]','$_POST[username]','$_POST[id]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Welcome New User";

mysql_close($con)
?> 

Link to comment
Share on other sites

Missing a line terminating semicolon on the previous line. Also, even though it isn't technically necessary, it's a good idea to enclose the statements in curly braces. It helps readability immensely.

 

 

OOo wow :) way over my head. Can you show me or at leat point me to the direction i need to goto to learn?

Link to comment
Share on other sites

hmm i must be off somewhere because now it says welcome user and puts nothing in the table. Even if the user exist i still get a Welcome new user . Well it does put something in the table...blankness. Just a blank entry :confused:

 

<?php
$con = mysql_connect("yesmyserver","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbtest", $con);

$query  = "SELECT username, id, message FROM partylist";
if ($query =='username')
    echo "User already exist";
      
  
else
  
{ 
$sql="INSERT INTO partylist (RecordID, Username, ID)
VALUES
('$_POST[firstname]','$_POST[username]','$_POST[id]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Welcome New User";
}

mysql_close($con)
?> 

Link to comment
Share on other sites

Correction, my html was off where i renamed the vaules. It post now. The only catch is it it always post and always says welcome new user. I think im useing the incrorrect vaule at the

 if ($query =='[username]')

part but not sure what the correct would be.

 

<?php
$con = mysql_connect("wserver.com","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbtest", $con);

$query  = "SELECT username, id, message FROM partylist";
if ($query =='[username]')
    echo "User already exist";
      
  
else
  
{ 
$sql="INSERT INTO partylist (RecordID, Username, ID)
VALUES
('$_POST[firstname]','$_POST[username]','$_POST[id]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "Welcome New User";
}

mysql_close($con)
?> 

Link to comment
Share on other sites

OK, let's start with this. It doesn't do anything at all. The query is never executed with mysql_query(), and all that block of code does is check to see if the query string equals the string [username], which it obviously does not.

 

$query  = "SELECT username, id, message FROM partylist";
if ($query == '[username]') {
echo "User already exist";

 

Also, I'm moving this thread to PHP Coding help, since this is ultimately a PHP issue.

Link to comment
Share on other sites

oo hey i didnt kow you where in this section too :) I don't fully understand how to correct it. I know i want it to search for the username to see if it exist but that was the best i could come up with and am not sure how to make it "do" something. I have made the username via the html then tried the same username agin. Says welcome still and then i check the database and the username is already there.

Link to comment
Share on other sites

Let's start here:

$query  = "SELECT username, id, message FROM partylist";
if ($query == '[username]') {
     echo "User already exist";
}

Right now, what that chunk of code says is:

"If the string: "SELECT username, id, message FROM partylist" is literally the same string as: "[username]", echo to the browser the string: "User already exist.

 

Do you have an understanding of how arrays and forms work, and what happens when a form is submitted? If not, we should start there, or you're just going to end up more confused.

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.