how to save value from prompt to database in php

Recommended Answers

If you have to add row in database, you can use following code

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);

    /* Add new row in table FRUIT */
    $count = $dbh->exec("INSERT INTO fruit (id,name) VALUES(1,'banana')");

    /* Show count …
Jump to Post

All 2 Replies

If you have to add row in database, you can use following code

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);

    /* Add new row in table FRUIT */
    $count = $dbh->exec("INSERT INTO fruit (id,name) VALUES(1,'banana')");

    /* Show count add rows */
    print("Add $count rows.\n");

} catch (PDOException $e) {
    echo 'Don\'t connect: ' . $e->getMessage();
}

MYSQL Version:

$yourVar = "blabla";

// you can escape your string by mysql_real_escape_string($yourVar);

$sql = mysql_query("INSERT INTO yourTable (yourField1) values('".$yourVar."') ");
if(!$sql) {
    echo "Insert failed - ".mysql_error($sql);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.