Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    cant connect php to database

    Hi all! I would like to ask for help. i have a code that i got from youtube. my problem is i cant connect to my database and i dont know why. i dont think that i have a problem in my code. i have 2 php file. reg.php for the connection and demo-form.php for adding the record.

    demo-form.php
    PHP Code:
    <form action="reg.php" method="post" />
    <
    p>First Name: <input type="text" name="fname" /> </p>
    <
    p>Middle Name: <input type="text" name="mname" /> </p>
    <
    p>Last Name: <input type="text" name="lname" /> </p>
    <
    p>Home address: <input type="text" name="homeadd" /> </p>
    <
    p>Email address: <input type="text" name="emailadd" /> </p>
    <
    p>Office/project number: <input type="text" name="ofcnum" /> </p>
    <
    p>Mobile number: <input type="text" name="mobilenum" /> </p>
    <
    p>Home number: <input type="text" name="homenum" /> </p>
    <
    p>Position
    <
    select name="position">
      <
    option value ="">Select</option>
      <
    option value ="Project Manager">Project Manager</option>
      <
    option value ="Practice Lead">Project Manager</option>
      <
    option value ="Team Leader">Team Leader</option>
      <
    option value ="Team Member">Team Member</option>
    </
    select>
    <
    p>Practice
    <
    select name="position">
      <
    option value ="">Select</option>
      <
    option value ="Solutions">Solutions</option>
      <
    option value ="Outsourcing">Outsourcing</option>
     </
    select>
    <
    p>Project Name: <input type="text" name="projname" /> </p>
    <
    p>Project Manager: <input type="text" name="projmgr" /> </p>
    <
    p>Team Leader: <input type="text" name="teamlead" /> </p>

    <
    input type="submit" value="Submit" />
    <
    body bgcolor="cccccc">
    </
    form

    reg.php

    PHP Code:
    <?php[/b]


    [
    b]define ('DB_NAME''emp');
    define ('DB_USER''root');
    define ('DB_PASSWORD''');
    define ('DB_HOST''localhost');[/b]


    [
    b]$link mysql_connect(DB_HOSTDB_USERDB_PASSWORD);[/b]


    [
    b]if (!$link) {
    die(
    'Could not connect: ' mysql_error());
    }[/
    b]


    [
    b]$db_selected mysql_select_db(DB_NAME$link);[/b]


    [
    b]if (!$db_selected) {
    die(
    'Can\'t use ' DB_NAME ': ' mysql_error());
    }[/
    b]


    [
    b]$value1 $_POST["fname"];
    $value2 $_POST["mname"];
    $value3 $_POST["lname"];
    $value4 $_POST["homeadd"];
    $value5 $_POST["emailadd"];
    $value6 $_POST["ofcnum"];
    $value7 $_POST["mobilenum"];
    $value8 $_POST["homenum"];
    $value9 $_POST["position"];
    $value10 $_POST["practice"];
    $value11 $_POST["projname"];
    $value12 $_POST["projmgr"];
    $value13 $_POST["teamlead"];[/b]


    [
    b]$sql "INSERT INTO employee (fname, mname, lname, homeadd, emailadd, ofcnum, mobilenum, homenum, position, projname, projmgr, teamlead) 
      VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12')"
    ;
     
    if (!
    mysql_query($sql)) {
    die(
    'Error: ' mysql_error());
    }[/
    b]


    [
    b]mysql_close();[/b]


    [
    b]?>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,859
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Have your got any errors?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    434
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Here's my 2pence worth...

    try changing the inset line to this...

    Code:
    $sql = "INSERT INTO `employee` (`fname`, `mname`, `lname`, `homeadd`, `emailadd`, `ofcnum`, `mobilenum`, `homenum`, `position`, `projname`, `projmgr`, `teamlead`) 
      VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12')";


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •