简体   繁体   中英

Unable to connect to mySQL database

Im new to coding and am unable to connect to the database i have created and i really cannot move further because i need to access that in order to add , display and manipulate data . could someone please help me understand what it is i am doing wrong ? ????? my php and html form are given below . i have created a database with a table but information doesnt get added to it .

 <form id="details" action="Seller.php" method="post"> <div class="textdiv"> Enter the title for the advertisemennt <input type="text" id="adtitle"></input><br/> Sellers name <input type="text" id="sellersname"> </input><br/> Email id <input type="text" id="email"></input><br/> Enter A brief description of the necessary details for the product <textarea rows="10" cols="30" id="description"></textarea><br/> <br/> Choose a category <select name="product"> <option value="Electronics">Electronics</option> <option value="Cars">Cars</option> <option value="pets">pets</option> <option value="furniture">furniture</option> <option value="appliances">appliances</option> <option value="books">books</option> <option value="other">other</option> </select> <br/> <br/> Enter your mobile number <input type="text" id="phone number"> </input> <br/> <br/> your phone number will be kept private .<br/> <br/> Selling price <input type="text" id="base"></input><br> Cutoff price ( the price below which you will not sell ) <input type="text" id="cutoff"></input><br> </form></div> 
 <?php define (DB_NAME,'seller'); define (DB_USER,'root'); define (DB_PASSWORD,''); define (DB_HOST,'localhost'); $link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); if (!$link) {die('Could not connect: '.mysql_error());} $db_selected = mysql_select_db(DB_NAME,$link); if(!$db_selected){die('Cant use '.DB_NAME.':'.mysql_error();} echo'SUCESS'; $value = $_POST['adtitle']; $value2 = $_POST['sellersname']; $value3 = $_POST['email']; $value4 = $_POST['description']; $value5 = $_POST['category']; $value6 = $_POST['phone number']; $value7 = $_POST['base']; $value8 = $_POST['cutoff']; $sql ="INSERT INTO seller (adtitle,sellersname,email,description,category,phone number,base,cutoff) VALUES('$value',$value2,$value3,$value4,$value5,$value6,$value7,$value8)"; mysql_close(); ?> 

Try this code for connecting to mysql database.

$serverName = "localhost";
$userName = "root";
$password = "";
$database = "seller";

$db = new mysqli( $serverName, $userName, $password, $database );

if ( $db -> connect_errno ) {
    echo "Failed to connect to MySQL: (" . $db -> connect_errno . ") " . $db -> connect_error;
} else {
    echo "You are connected to your database. <br><br>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM